std.fs

Files and directories:

import std.fs

fs.create_dir("out/logs")?
fs.write("out/logs/today.txt", "started\n")?
fs.append("out/logs/today.txt", "done\n")?
for name in fs.list("out")? {
    print(name)
}

Paths are relative to the working directory. Errors are messages such as cannot list directory "out": not found.

Functions

fn exists

pub fn exists(path: string) -> bool

Whether a file or directory exists at path.

fn is_dir

pub fn is_dir(path: string) -> bool

Whether path is a directory.

fn is_file

pub fn is_file(path: string) -> bool

Whether path exists and is not a directory.

fn list

pub fn list(dir: string) -> Result[List[string], string]

The names of the files and directories in dir, sorted, without . and ...

fn create_dir

pub fn create_dir(path: string) -> Result[bool, string]

Creates a directory and any missing parents. Ok(false) when it already existed.

fn remove

pub fn remove(path: string) -> Result[bool, string]

Removes a file or an empty directory. Ok(false) when there was nothing to remove.

fn remove_all

pub fn remove_all(path: string) -> Result[bool, string]

Removes a file, or a directory with everything in it. Ok(false) when there was nothing to remove.

fn rename

pub fn rename(from: string, to: string) -> Result[bool, string]

Renames or moves a file or directory.

fn size

pub fn size(path: string) -> Result[i64, string]

A file's size in bytes.

fn modified

pub fn modified(path: string) -> Result[time.Time, string]

When a file was last changed.

fn read

pub fn read(path: string) -> Result[string, string]

The text of a file, which must be UTF-8 (the same as read_file).

fn write

pub fn write(path: string, text: string) -> Result[i64, string]

Replaces a file's text, creating the file if needed (the same as write_file); the number of bytes written.

fn append

pub fn append(path: string, text: string) -> Result[i64, string]

Adds text at the end of a file, creating the file if needed; the number of bytes written.