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:
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"])))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:
try File.write("out.txt", "first")
println(File.write("out.txt", "second", replace: false))
println(try File.read("out.txt"))Err{ error: Error{ message: "open out.txt: file exists" } }
firstA 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.
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"))north,12
south,7A 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:
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") // falseFile.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:
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")
}index.md: 7 bytes
posts/first.md: 13 bytesA 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:
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)600
trueFunctions by job
Whole files
- File.read, File.readBytes: the contents of a file, as text or as bytes
- File.write, File.writeBytes: create a file, or replace what it held
Building and taking paths apart
- Path.join: join parts with the separator, cleaned
- Path.clean: the shortest equivalent path
- Path.dir: all but the last element
- Path.base: the last element
- Path.ext: the extension of the last element
Absolute and relative paths
- Path.isAbs: whether a path is absolute
- Path.abs: a path made absolute against the working directory
- Path.rel: a target as a path from a base
- Os.cwd, Os.home: the working directory and the home directory
- Os.cacheDir, Os.configDir: where the user's programs keep what can be made again, and their configuration
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
- File.Handle: an open file, a standard stream, a connection, or a buffer over one
- File.open: open a file for reading
- File.create: create or truncate a file for writing
- File.append: open a file for writing at its end
- File.close: close a handle, writing out a buffer first
- Os.stdin, Os.stdout, Os.stderr: the standard streams as handles
- File.isTerminal: whether a handle is a terminal
Reading and writing a handle
- File.readLine: the next line, or
nullat the end - File.readAll: the rest of the input as text
- File.readChunk: what is there, up to a number of bytes
- File.readExactly: exactly a number of bytes
- File.writeLine, File.writeText: text, with or without a newline
- File.writeChunk: bytes as they are
- File.pipe: copy the rest of one handle to another
- File.buffered: a handle that holds its writes until full, flush, or close
- File.flush: hand what a buffered handle holds to the OS
- File.sync: flush, then have the OS write the file to storage, so it survives a crash
Metadata and modes
- File.stat: the File.Info of a path: name, size, kind, modified time, mode, and owner ids
- File.chmod: set the mode
- File.chown: set the user and group that own a file, by id
- File.touch: set the modified time, creating an empty file when missing
Creating, moving, copying, and removing
- File.mkdir: make a directory with its parents
- File.rename: move a file or directory
- File.copy: copy one file's contents and mode
- File.copyAll: copy a file or a whole tree
- File.remove: remove a file or an empty directory
- File.removeAll: remove a whole tree
Links
- File.symlink: make a symbolic link
- File.readLink: the target of a link, as stored
- File.realPath: resolve every link along a path
Temporary files and locks
- File.tempFile, File.tempDir: a new file or directory under a free name, only for you
- File.lock: open a file holding its lock, waiting for it
- File.tryLock: the same without waiting, or
nullwhen another handle holds it
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
