fs
Files and folders: read, write, list, walk, copy and remove. Relative paths start from the folder you ran tb in; fs.here(name) gives a path next to your program.
In the playground, files live in a pretend folder inside your browser that holds the sample data in apps/data/. Nothing touches your real disk.
import fs
Example
import fs
fs.write("notes.txt", "first line\n")
fs.append("notes.txt", "second line\n")
say(fs.lines("notes.txt"))
say(fs.size("notes.txt"), "bytes")
say(fs.ext("apps/data/auth.log"), fs.name("apps/data/auth.log"))
say(fs.list("apps/data").filter(fn(name) { return name.ends_with(".log") }))
fs.remove("notes.txt")
say(fs.exists("notes.txt"))
Functions
read(path)
returns textThe whole text file.
lines(path)
returns listThe text file as a list of lines (without line endings).
write(path, text)
Save text in a file, replacing what was there (makes the file if needed).
append(path, text)
Add text to the end of a file (makes the file if needed). Add "\n" yourself for new lines.
exists(path)
returns boolTrue if there is a file or folder there.
is_dir(path)
returns boolTrue if path is a folder.
is_file(path)
returns boolTrue if path is a file.
list(folder?)
returns listThe names of the files and folders inside (sorted). Default: the current folder.
walk(folder?)
returns listThe paths of every file inside the folder and its sub-folders (sorted).
mkdir(path)
Make a folder (and any missing folders above it). Fine if it already exists.
remove(path)
Delete a file. (It won't delete folders, to keep you safe.)
copy(from, to)
Copy a file. If to is a folder, the copy goes inside it.
size(path)
returns numberThe file's size in bytes.
modified(path)
returns numberWhen the file last changed, as a timestamp (see the time library).
home()
returns textYour home folder.
cwd()
returns textThe current folder (where relative paths start).
here(name?)
returns textThe folder your program is in, or the path of name inside it: fs.read(fs.here("data.txt")).
join(part, ...)
returns textJoin path parts with the right slash for this computer: join("logs", "auth.log").
name(path)
returns textThe last part of a path: name("logs/auth.log") is "auth.log".
ext(path)
returns textThe file extension with its dot: ext("auth.log") is ".log" ("" if none).
parent(path)
returns textThe folder that holds path: parent("logs/auth.log") is "logs".