Skip to content

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))
Output
reports/2026/summary.txt
summary.txt
summary
txt
reports/2026

Use Path.join rather than gluing pieces together with +, so the separators are always right. The paths Path makes go straight into File and Directory.

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

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"]))
Output
data/2026/report.txt
data

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"))
Output
notes.txt

Path.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"))
Output
cat
archive.tar

Path.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") == "")
Output
jpg
true

Path.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") == "")
Output
reports/2026
true

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"))
Output
true
false

Path.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")))
Output
true

Raises a FileError when nothing exists at path.