Pelago
stdInput, output and files

std.fs

Read and write files by path.

import std.fs

Paths are UTF-8 text, passed to the operating system unchanged. No function stops the program: each reports a failure as an io_error value, which has a kind (notFound, denied, invalid or other) and a message. Use ? to pass it on, or switch to handle it.

FunctionResultNotes
fs.readText(path)string ! io_errorThe whole file. Text that is not valid UTF-8 is an error.
fs.readBytes(path)[byte] ! io_errorThe whole file as raw bytes.
fs.writeText(path, text)void ! io_errorCreate or replace the file.
fs.appendText(path, text)void ! io_errorAdd to the end, creating the file if needed.
fs.open(path)io.file ! io_errorOpen an existing file for reading.
fs.create(path)io.file ! io_errorCreate or replace a file for writing.
fs.append(path)io.file ! io_errorOpen for writing at the end.

An open io.file reads and writes in pieces. It is closed when it goes out of scope.

MethodResultNotes
readLine!()string? ! io_errorThe next line; none at the end of the file.
readAll!()string ! io_errorThe rest of the file.
readBytes!()[byte] ! io_errorThe rest of the file as bytes.
write!(text)void ! io_errorWrite text.
import std.fs

fn main() -> void ! io_error {
    fs.writeText("notes.txt", "one\ntwo\n")?
    var f = fs.open("notes.txt")?
    var more = true
    while more {
        switch f.readLine!()? {
            case some(line): print(line)
            case none: more = false
        }
    }
}

A missing file is an ordinary value:

import std.fs

fn main() {
    switch fs.readText("this-file-does-not-exist.txt") {
        case ok(text): print(text)
        case err(e): print("failed: #(e.kind)")
    }
}
failed: notFound

Files and folders

FunctionResultNotes
fs.exists(path)boolWhether the path exists. It never fails.
fs.metadata(path)fs.file_info ! io_errorA size in bytes and a kind: fs.file_kind.file, dir or other. A link is followed.
fs.createDir(path)void ! io_errorMake one folder.
fs.createDirs(path)void ! io_errorMake a folder and any missing parents. A folder that is already there, even one another process makes at the same moment, is not an error.
fs.removeFile(path)void ! io_errorDelete a file.
fs.removeDir(path)void ! io_errorDelete an empty folder.
fs.rename(from, to)void ! io_errorMove or rename.
fs.readDir(path)[string] ! io_errorThe names in a folder, sorted.

The path and folder calls, readDir included, work in the interpreter and on Linux (x86-64 and AArch64), Windows (x86-64 and ARM64) and macOS (AArch64). The macOS and Windows ARM64 paths are tested only by the project's CI jobs for those systems.

import std.fs

fn main() -> void ! io_error {
    fs.createDirs("tmp-docs-demo/a/b")?
    fs.writeText("tmp-docs-demo/a/one.txt", "hello")?
    fs.writeText("tmp-docs-demo/a/two.txt", "hi")?
    const here = fs.exists("tmp-docs-demo/a/one.txt")
    const gone = fs.exists("tmp-docs-demo/nope")
    print("#(here) #(gone)")
    const info = fs.metadata("tmp-docs-demo/a/one.txt")?
    print("#(info.size) #(info.kind == fs.file_kind.file)")
    fs.rename("tmp-docs-demo/a/two.txt", "tmp-docs-demo/a/three.txt")?
    for name in fs.readDir("tmp-docs-demo/a")? {
        print(name)
    }
    fs.removeFile("tmp-docs-demo/a/one.txt")?
    fs.removeFile("tmp-docs-demo/a/three.txt")?
    fs.removeDir("tmp-docs-demo/a/b")?
    fs.removeDir("tmp-docs-demo/a")?
    fs.removeDir("tmp-docs-demo")?
}
true false
5 true
b
one.txt
three.txt

Standard input has the same shape in std.io: io.readLine(), io.readAll() and io.readBytes() never stop the program on a read error, and io.tryReadLine() and io.tryReadAll() return the io_error instead.

On this page