File System Library (fs)
fs reads and writes files. It's off by default, along with the environment functions and importing files from disk, so a document can only read and change itself. The host turns them all on together:
# Python
doc.allow_system()
// Rust
graph.allow_system();
The stof CLI turns this on, since it runs your own files. The JavaScript package doesn't include fs, so the examples below don't run on this site. If a document should never touch the disk, don't call allow_system(), and a document that tries gets an error.
Paths are relative to the process's working directory.
fs::read(path: str) -> blob
Reads a file into a binary blob.
const bytes = fs::read("src/lib.rs");
fs::read_string(path: str) -> str
Same as fs.read, but as a string instead of raw bytes.
const content = fs::read_string("src/lib.rs");
fs::write(path: str, content: str | blob) -> void
Writes to a file, overwriting it if it already exists. Throws if the containing directory doesn't exist.
fs::write("src/text.txt", "testing");