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)
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
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
- std.path — path joining and decomposition
- std.json — structured read/write via
parse/stringify - std.compression —
compress_file/decompress_file