Directory
Standard library namespace · Always available, with no import
Directory works with folders: making them, seeing what is inside, and removing them. What is
inside a folder is read and written with File.
Directory.create("reports/2026")File.write("reports/2026/summary.txt", "All good")File.write("reports/overview.txt", "See 2026")
for path in Directory.list("reports").sort() { print(path)}reports/2026reports/overview.txtA path is a String, and a relative one is looked up from the folder the program was run in.
When an operation fails, it raises a FileError.
At a glance
Section titled “At a glance”| Looking | |
|---|---|
Directory.exists?(path) |
Whether there is a folder at the path |
Directory.list(path) |
The paths inside a folder |
| Changing | |
|---|---|
Directory.create(path) |
Make a folder, and any it needs |
Directory.delete(path) |
Remove an empty folder |
Directory.delete_recursive(path) |
Remove a folder and everything in it |
Looking
Section titled “Looking”Directory.exists?(path: String): Bool
Whether there is a folder at path. A file is not a folder, so for a file this is false; ask
File.exists? instead.
Directory.list(path: String): List[String]
The full path of everything directly inside the folder, files and folders alike. Each path starts
with the path you gave, so it can be passed straight to File.read or to Directory.list again.
It does not look inside the folders it finds.
The order is not specified, and can differ between computers, so sort the list when the order matters, as above.
Raises a FileError when there is no folder at path.
Changing
Section titled “Changing”Directory.create(path: String)
Makes the folder, along with any folders above it that are missing, so Directory.create("a/b/c")
works even when a doesn’t exist yet. If the folder is already there, nothing happens.
Raises a FileError when the folder can’t be made, such as when a file already
has that name.
Directory.delete(path: String)
Removes a folder, but only an empty one. A folder with something in it is left alone, and the error says so:
Directory.create("old")File.write("old/keep.txt", "keep me")
try { Directory.delete("old")}catch error: FileError { print(error.message)}`old` is not empty, so it cannot be deletedRaises a FileError when the folder isn’t empty, doesn’t exist, or can’t be
removed.
Directory.delete_recursive(path: String)
Removes a folder and everything inside it, folders and files alike. There is no way to undo it, so be sure of the path.
If the folder is already gone, nothing happens, so it is safe to call twice, and a program can use it to clear out a folder without checking first.
Directory.create("scratch/deep")File.write("scratch/deep/file.txt", "temporary")
Directory.delete_recursive("scratch")Directory.delete_recursive("scratch")print(Directory.exists?("scratch"))falseRaises a FileError when something inside can’t be removed.