std.file

std.file provides file and directory operations — read/write, list, glob,
temp files, and metadata.

All paths accept ~ and $VAR / ${VAR} expansion automatically
(%VAR% on Windows).

Reference

read(path)            # entire contents as a string
read_lines(path)      # array of lines
write(path, content)  # overwrite
write_lines(path, lines)  # array of strings joined with \n (no trailing newline)
append(path, content)
exists(path)  size(path)  is_file(path)  is_dir(path)
remove(path)  copy(src, dst)  rename(old, new)  mkdir(path)
list(path)            # entries in the directory
glob(pattern)         # entries matching * / ? / [set] wildcards
temp([suffix])        # path of a new empty temp file (see below)

exists / is_file / is_dir return booleans (false for missing paths,
never an error); size returns the byte count and throws FILE_NOT_FOUND
on a missing path.
write_lines takes an array of strings and joins them with single \n
separators — no trailing newline is added. temp creates an empty file in
the system temp directory (default suffix .tmp, e.g. temp(".log")) with
a unique scii_<pid>_<counter> name; it is not auto-deleted — remove it
yourself when done.

Examples

Read / write / append

# read a text file
var txt = std.file.read("~/data/notes.txt")
print(txt)

# overwrite it (or append)
std.file.write("out.txt", std.string.upper(txt))
std.file.append("out.txt", "\nprocessed\n")

# read back line by line
var lines = std.file.read_lines("out.txt")
print("file has", len(lines), "lines")

Inspect and clean up

var path = std.file.temp(".log")     # new empty temp file (exists on disk)
print(std.file.exists(path))         # true
std.file.write(path, "line one\nline two\n")
std.file.append(path, "line three\n")
var lines = std.file.read_lines(path)
print(lines)                         # ["line one", "line two", "line three"]

var info = std.file.size(path)
print("size in bytes:", info)
std.file.remove(path)
print(std.file.exists(path) == false)  # true

List and glob

var entries = std.file.list(".")         # entries in a directory
for (f : entries) { print(f) }

var cpp = std.file.glob("src/*.cpp")     # * / ? / [set] wildcards
print(cpp)                               # every matching path

print(std.file.is_file("a.txt"))
print(std.file.is_dir("src"))
std.file.mkdir("out/data")
std.file.copy("a.txt", "b.txt")
std.file.rename("b.txt", "c.txt")

See also