Path
Standard library namespace · Always available, with no import
A path is the text that says where a file or folder is, such as reports/2026/summary.txt. Path
builds paths from pieces and takes them apart again, without touching the disk: it only works with
the text, so it is fine to use on a path that doesn’t exist yet.
const file = Path.join(["reports", "2026", "summary.txt"])print(file)print(Path.name(file))print(Path.stem(file))print(Path.extension(file))print(Path.parent(file))reports/2026/summary.txtsummary.txtsummarytxtreports/2026Use Path.join rather than gluing pieces together with +, so the separators are always right.
The paths Path makes go straight into File and Directory.
At a glance
Section titled “At a glance”| Building | |
|---|---|
Path.join(parts) |
Pieces put together into one path |
| Taking apart | |
|---|---|
Path.name(path) |
The last part, with its extension |
Path.stem(path) |
The last part, without its extension |
Path.extension(path) |
The extension, without the dot |
Path.parent(path) |
Everything before the last part |
| Absolute paths | |
|---|---|
Path.absolute?(path) |
Whether the path starts from the top of the disk |
Path.absolute(path) |
The full path to something that exists |
Building
Section titled “Building”Path.join(parts: List[String]): String
The parts put together with a separator between each.
print(Path.join(["data", "2026", "report.txt"]))print(Path.join(["data"]))data/2026/report.txtdataTaking apart
Section titled “Taking apart”Path.name(path: String): String
The last part of the path: the file or folder name, with its extension.
print(Path.name("/home/ada/notes.txt"))notes.txtPath.stem(path: String): String
The last part without its extension. Only the final extension is removed, so archive.tar.gz has the
stem archive.tar.
print(Path.stem("photos/cat.jpg"))print(Path.stem("archive.tar.gz"))catarchive.tarPath.extension(path: String): String
The part after the last dot of the name, without the dot: "txt", not ".txt". It is "" for a name
that has no extension.
print(Path.extension("photos/cat.jpg"))print(Path.extension("Makefile") == "")jpgtruePath.parent(path: String): String
Everything before the last part: the folder a file or folder is in. It is "" when the path is only
a name, with no folder in front of it.
print(Path.parent("reports/2026/summary.txt"))print(Path.parent("notes.txt") == "")reports/2026trueAbsolute paths
Section titled “Absolute paths”A path that starts from the top of the disk, such as /home/ada/notes.txt, is absolute. One that
doesn’t, such as notes.txt, is relative, and means “from the folder the program was run in”.
Path.absolute?(path: String): Bool
Whether the path is absolute. It only reads the text of the path, so it works for a path that doesn’t exist.
print(Path.absolute?("/home/ada/notes.txt"))print(Path.absolute?("notes.txt"))truefalsePath.absolute(path: String): String
The full, absolute path to something that exists, starting from the top of the disk. Unlike the
rest of Path, it looks at the disk, and needs the file or folder to be there.
File.write("notes.txt", "hello")print(Path.absolute?(Path.absolute("notes.txt")))trueRaises a FileError when nothing exists at path.