std.fs
Path-level file system operations: whole-file reading and writing, directory management and traversal, file metadata, temporary files, and path operations.
Use std.os when incremental reads and writes through a handle (open/read/seek) are required; use std.io for console input and output.
use std.fsPlatform Availability
This module is not exported on the wasm32 target (there are no file system semantics). mkdtemp / tmpfile / temp_dir depend on secure temporary file creation and are available only on native targets.
Function Overview
| Function | Signature |
|---|---|
read_file | (path: &String) -> String |
write_file | (path: &String, content: &String) -> Bool |
append_file | (path: &String, content: &String) -> Bool |
exists | (path: &String) -> Bool |
is_file | (path: &String) -> Bool |
is_dir | (path: &String) -> Bool |
mkdir | (path: &String) -> Bool |
mkdir_all | (path: &String) -> Bool |
rmdir | (path: &String) -> Bool |
remove | (path: &String) -> Bool |
copy | (src: &String, dst: &String) -> Bool |
rename | (src: &String, dst: &String) -> Bool |
read_dir | (path: &String) -> Vec(String) |
walk | (path: &String) -> Vec(String) |
stat | (path: &String) -> Dict(String, Any) |
temp_dir | () -> String |
mkdtemp | (prefix: &String) -> String |
tmpfile | (prefix: &String) -> String |
path_join | (base: &String, rel: &String) -> String |
path_basename | (path: &String) -> String |
path_dirname | (path: &String) -> String |
path_extension | (path: &String) -> String |
Functions
read_file
read_file: (path: &String) -> StringReads the entire contents of a file at once as a string.
path— File path (read-only borrow)
Returns: The entire contents of the file. Error: Throws E6007 if the file does not exist or access is denied. Never returns an empty string.
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_read_file.txt"
fs.write_file(p, "hello")
content = fs.read_file(p)
assert(content == "hello")
fs.remove(p)
}write_file
write_file: (path: &String, content: &String) -> BoolWrites content to path, overwriting any existing contents; creates the file if it does not exist.
path— File path (read-only borrow)content— Content to write (read-only borrow)
Returns: true if the write succeeds. Error: Throws E6007 if the directory does not exist or access is denied (does not return false).
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_write_file.txt"
ok = fs.write_file(p, "hello")
assert(ok)
assert(fs.exists(p))
fs.remove(p)
}append_file
append_file: (path: &String, content: &String) -> BoolAppends content to the end of path; creates the file if it does not exist.
Returns: true if the write succeeds. Error: Throws E6007 if access is denied.
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_append_file.txt"
fs.write_file(p, "hello")
fs.append_file(p, " world")
assert(fs.read_file(p) == "hello world")
fs.remove(p)
}exists
exists: (path: &String) -> BoolWhether the path exists (whether it is a file or a directory).
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_exists.txt"
assert(!fs.exists(p), "absent before write")
fs.write_file(p, "x")
assert(fs.exists(p), "present after write")
fs.remove(p)
}is_file
is_file: (path: &String) -> BoolWhether the path points to a regular file. Returns false if the path does not exist (no error is raised).
is_dir
is_dir: (path: &String) -> BoolWhether the path points to a directory. Returns false if the path does not exist (no error is raised).
mkdir
mkdir: (path: &String) -> BoolCreates a single-level directory; the parent directory must already exist. To create directories recursively, use mkdir_all.
Returns: true on success. Error: Throws E6007 if the parent directory is missing or the directory already exists.
mkdir_all
mkdir_all: (path: &String) -> BoolRecursively creates a directory, creating any missing parent directories as well; an existing directory is treated as a success.
Returns: true on success. Error: Throws E6007 if access is denied.
use std.assert
use std.fs
main: () -> Void = {
d = "__yx_doc_fs_mkdir_all/a/b"
ok = fs.mkdir_all(d)
assert(ok)
assert(fs.is_dir(d))
fs.rmdir("__yx_doc_fs_mkdir_all/a/b")
fs.rmdir("__yx_doc_fs_mkdir_all/a")
fs.rmdir("__yx_doc_fs_mkdir_all")
}rmdir
rmdir: (path: &String) -> BoolDeletes an empty directory. Deleting a non-empty directory fails and throws E6007.
remove
remove: (path: &String) -> BoolDeletes a file. To delete a directory, use rmdir (empty directories only).
Returns: true on success. Error: Throws E6007 if the file does not exist.
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_remove.txt"
fs.write_file(p, "x")
assert(fs.remove(p), "remove ok")
assert(!fs.exists(p), "gone after remove")
}copy
copy: (src: &String, dst: &String) -> BoolCopies the file contents and permission bits to the destination path; an existing destination is overwritten (consistent with Rust fs::copy, metadata timestamps are not copied).
Returns: true on success. Error: Throws E6007 if the source does not exist.
rename
rename: (src: &String, dst: &String) -> BoolRenames or moves a file or directory. Throws E6007 when moving across devices (use copy followed by remove instead).
Returns: true on success.
use std.assert
use std.fs
main: () -> Void = {
a = "__yx_doc_fs_rename_a.txt"
b = "__yx_doc_fs_rename_b.txt"
fs.write_file(a, "data")
assert(fs.rename(a, b), "rename ok")
assert(fs.exists(b), "target exists")
assert(!fs.exists(a), "source gone")
fs.remove(b)
}read_dir
read_dir: (path: &String) -> Vec(String)Lists the names of entries in a directory (without path prefixes), returning List(String) sorted by name.
Unlike the original
std.os.read_dir(which returned a string joined by"\n"), this function returns a correctly typed List—a semantic upgrade from whenstd.osfile operations were moved intostd.fs.
use std.assert
use std.fs
use std.list
main: () -> Void = {
d = "__yx_doc_fs_readdir"
fs.mkdir_all(d)
fs.write_file(d + "/b.txt", "b")
fs.write_file(d + "/a.txt", "a")
names = fs.read_dir(d)
assert(list.len(names) == 2, "two entries")
assert(list.get(names, 0) == "a.txt", "sorted first")
assert(list.get(names, 1) == "b.txt", "sorted second")
fs.remove(d + "/a.txt")
fs.remove(d + "/b.txt")
fs.rmdir(d)
}walk
walk: (path: &String) -> Vec(String)Recursively traverses a directory tree, returning the full paths of all entries as List(String); each level is sorted by name, and directories appear before their contents (depth first). Symbolic links themselves are listed as entries but are not traversed.
Error: Throws E6007 when path is not a directory.
use std.assert
use std.fs
use std.list
main: () -> Void = {
d = "__yx_doc_fs_walk"
fs.mkdir_all(d + "/sub")
fs.write_file(d + "/a.txt", "a")
fs.write_file(d + "/sub/b.txt", "b")
paths = fs.walk(d)
// a.txt, the sub directory, and sub/b.txt: three entries
assert(list.len(paths) == 3, "2 files + 1 dir")
fs.remove(d + "/a.txt")
fs.remove(d + "/sub/b.txt")
fs.rmdir(d + "/sub")
fs.rmdir(d)
}stat
stat: (path: &String) -> Dict(String, Any)Reads file or directory metadata and returns a dictionary:
| Key | Type | Meaning |
|---|---|---|
size | Int | Number of bytes |
is_dir | Bool | Whether it is a directory |
is_file | Bool | Whether it is a regular file |
readonly | Bool | Whether it has read-only permissions |
mtime | Int | Modification time (Unix seconds) |
Error: Throws E6007 if the path does not exist.
use std.assert
use std.fs
main: () -> Void = {
p = "__yx_doc_fs_stat.txt"
fs.write_file(p, "12345")
st = fs.stat(p)
assert(st["size"] == 5, "byte size")
assert(st["is_file"], "is a file")
fs.remove(p)
}temp_dir
temp_dir: () -> StringThe system temporary directory path (%TEMP% on Windows, $TMPDIR or /tmp on Unix).
mkdtemp
mkdtemp: (prefix: &String) -> StringCreates a unique temporary directory inside the system temporary directory (with a name beginning with prefix) and returns its full path.
The directory is not automatically reclaimed: scripts should call
rmdirthemselves when finished. This behavior is intentional—an explicit lifetime is more predictable than an implicit drop hook.
Returns: The path of the new directory. Error: Throws E6007 if creation fails.
use std.assert
use std.fs
main: () -> Void = {
d = fs.mkdtemp("yx_doc_")
assert(fs.is_dir(d), "temp dir created")
fs.rmdir(d)
}tmpfile
tmpfile: (prefix: &String) -> StringCreates a unique empty temporary file inside the system temporary directory (with a name beginning with prefix) and returns its full path. Likewise, it is not automatically reclaimed; call remove when finished.
use std.assert
use std.fs
main: () -> Void = {
p = fs.tmpfile("yx_doc_")
assert(fs.is_file(p), "temp file created")
fs.write_file(p, "scratch")
assert(fs.read_file(p) == "scratch")
fs.remove(p)
}path_join
path_join: (base: &String, rel: &String) -> StringJoins path components. If rel is an absolute path, it directly replaces base (consistent with Rust/Python). The separator depends on the platform (\ on Windows).
use std.assert
use std.fs
use std.string
main: () -> Void = {
joined = fs.path_join("dir", "file.txt")
assert(string.ends_with(joined, "file.txt"), "suffix kept")
assert(string.starts_with(joined, "dir"), "base kept")
}path_basename
path_basename: (path: &String) -> StringThe final component of a path; returns an empty string when there is no final component (such as / or ..).
path_dirname
path_dirname: (path: &String) -> StringThe directory portion of a path; returns an empty string when there is no parent directory (such as a.txt).
use std.assert
use std.fs
main: () -> Void = {
assert(fs.path_basename("dir/file.txt") == "file.txt")
assert(fs.path_dirname("dir/file.txt") == "dir")
assert(fs.path_extension("a.tar.gz") == "gz")
assert(fs.path_extension("noext") == "")
}path_extension
path_extension: (path: &String) -> StringThe extension (without the dot, taken from the last one); returns an empty string when there is no extension.
Related
std.os— Handle-level incremental file I/O and environment variablesstd.io— Console input and output- Error code reference —
E6007general runtime error
