Skip to content

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.

yaoxiang
use std.fs

Platform 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 ​

FunctionSignature
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 ​

yaoxiang
read_file: (path: &String) -> String

Reads 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.

yaoxiang
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 ​

yaoxiang
write_file: (path: &String, content: &String) -> Bool

Writes 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).

yaoxiang
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 ​

yaoxiang
append_file: (path: &String, content: &String) -> Bool

Appends 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.

yaoxiang
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 ​

yaoxiang
exists: (path: &String) -> Bool

Whether the path exists (whether it is a file or a directory).

yaoxiang
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 ​

yaoxiang
is_file: (path: &String) -> Bool

Whether the path points to a regular file. Returns false if the path does not exist (no error is raised).

is_dir ​

yaoxiang
is_dir: (path: &String) -> Bool

Whether the path points to a directory. Returns false if the path does not exist (no error is raised).

mkdir ​

yaoxiang
mkdir: (path: &String) -> Bool

Creates 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 ​

yaoxiang
mkdir_all: (path: &String) -> Bool

Recursively 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.

yaoxiang
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 ​

yaoxiang
rmdir: (path: &String) -> Bool

Deletes an empty directory. Deleting a non-empty directory fails and throws E6007.

remove ​

yaoxiang
remove: (path: &String) -> Bool

Deletes a file. To delete a directory, use rmdir (empty directories only).

Returns: true on success. Error: Throws E6007 if the file does not exist.

yaoxiang
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 ​

yaoxiang
copy: (src: &String, dst: &String) -> Bool

Copies 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 ​

yaoxiang
rename: (src: &String, dst: &String) -> Bool

Renames or moves a file or directory. Throws E6007 when moving across devices (use copy followed by remove instead).

Returns: true on success.

yaoxiang
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 ​

yaoxiang
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 when std.os file operations were moved into std.fs.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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 ​

yaoxiang
stat: (path: &String) -> Dict(String, Any)

Reads file or directory metadata and returns a dictionary:

KeyTypeMeaning
sizeIntNumber of bytes
is_dirBoolWhether it is a directory
is_fileBoolWhether it is a regular file
readonlyBoolWhether it has read-only permissions
mtimeIntModification time (Unix seconds)

Error: Throws E6007 if the path does not exist.

yaoxiang
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 ​

yaoxiang
temp_dir: () -> String

The system temporary directory path (%TEMP% on Windows, $TMPDIR or /tmp on Unix).

mkdtemp ​

yaoxiang
mkdtemp: (prefix: &String) -> String

Creates 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 rmdir themselves 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.

yaoxiang
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 ​

yaoxiang
tmpfile: (prefix: &String) -> String

Creates 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.

yaoxiang
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 ​

yaoxiang
path_join: (base: &String, rel: &String) -> String

Joins path components. If rel is an absolute path, it directly replaces base (consistent with Rust/Python). The separator depends on the platform (\ on Windows).

yaoxiang
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 ​

yaoxiang
path_basename: (path: &String) -> String

The final component of a path; returns an empty string when there is no final component (such as / or ..).

path_dirname ​

yaoxiang
path_dirname: (path: &String) -> String

The directory portion of a path; returns an empty string when there is no parent directory (such as a.txt).

yaoxiang
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 ​

yaoxiang
path_extension: (path: &String) -> String

The extension (without the dot, taken from the last one); returns an empty string when there is no extension.