Skip to content

Files and paths ​

Two namespaces share the work. Path builds and takes apart paths as text and never reads the disk, so most of its functions return plain values. Path.abs, Path.rel, and Path.match return a Result: a working directory that cannot be read, a target with no relative path from the base, or a malformed pattern makes them fail. File reads, writes, and changes what is on disk, so every call that can fail returns a Result. You build a path with Path, then hand it to File:

ant
let dir = "notes"
try File.mkdir(dir)
let path = Path.join([dir, "todo.txt"])
try File.write(path, "buy milk\n")
println(path)
print(try File.read(path))
println(File.read(Path.join([dir, "done.txt"])))
text
notes/todo.txt
buy milk
Err{ error: Error{ message: "open notes/done.txt: no such file or directory" } }

A read of a missing file is an Err, and its message names the path. Only File.mkdir makes missing directories, with all their parents. File.write and the other calls that create a file are Err when the directory that would hold it is missing, so the example makes notes first.

File.write, File.writeBytes, File.create, and File.copy replace what is at the path. Pass replace: false to create the file only when nothing is there; otherwise the call is Err and changes nothing:

ant
try File.write("out.txt", "first")
println(File.write("out.txt", "second", replace: false))
println(try File.read("out.txt"))
text
Err{ error: Error{ message: "open out.txt: file exists" } }
first

A handle is open until it is closed. Defer the close right after opening, and close again at the end of a write: a close can fail, and a deferred close cannot report it.

ant
let rows = ["north,12", "south,7"]
let out = (try File.create("report.txt")).buffered()
defer { out.close() }
for row in rows {
  try out.writeLine(row)
}
try out.close()
print(try File.read("report.txt"))
text
north,12
south,7

A close hands the data to the OS but does not wait for the disk. When a file must survive a crash or a power loss, call try out.sync() before the final close, as when a temporary file is renamed into place: see Replace a file without a partial write.

A Path function works on the text alone, so the path does not have to exist:

ant
Path.join(["logs", "..", "app.log"]) // "app.log"
Path.base("reports/2026/march.csv")  // "march.csv"
Path.ext("reports/2026/march.csv")   // ".csv"
Path.isAbs("reports/2026/march.csv") // false

File.walk and File.glob give paths relative to the directory they search, so Path.join puts the directory back in front before another File call:

ant
try File.mkdir("site/posts")
try File.write("site/index.md", "# Home\n")
try File.write("site/posts/first.md", "# First post\n")
let dir = "site"
for path in try File.glob("**/*.md", dir: dir) {
  let info = try File.stat(Path.join([dir, path]))
  println("${path}: ${info.size} bytes")
}
text
index.md: 7 bytes
posts/first.md: 13 bytes

A path is used as written: ~ is not the home directory, and a relative path is taken against the working directory. Build a path under the home directory with Path.join([try Os.home(), ...]).

A mode is a number written in octal. On Unix, File.tempFile makes a file only you can read, with mode 0o600:

ant
let tmp = try File.tempFile("token-*", dir: ".")
defer { File.remove(tmp) }
let mode = (try File.stat(tmp)).mode
println(String.fromInt(mode, base: 8))
println(mode == 0o600)
text
600
true

Functions by job ​

Whole files ​

Building and taking paths apart ​

Absolute and relative paths ​

Finding files ​

  • File.exists: whether anything is at a path
  • File.list: the names in one directory
  • File.readDir: the entries of one directory, each a File.Entry with its name and whether it is a directory or a symbolic link
  • File.walk: every file below a directory
  • File.glob: the paths below a directory that match a pattern
  • Path.match: whether a path matches a pattern, without reading the disk

Opening and closing a handle ​

Reading and writing a handle ​

Metadata and modes ​

Creating, moving, copying, and removing ​

Temporary files and locks ​

See also ​

File · Path · Os · Read and write a whole file · Read a large file line by line · Walk a directory and match globs · Replace a file without a partial write · Use a temporary file or directory · Copy, move, and link files · Read and change modes, owners, and times · Lock a file between processes