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 text

The whole text file.

lines(path)

returns list

The 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 bool

True if there is a file or folder there.

is_dir(path)

returns bool

True if path is a folder.

is_file(path)

returns bool

True if path is a file.

list(folder?)

returns list

The names of the files and folders inside (sorted). Default: the current folder.

walk(folder?)

returns list

The 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 number

The file's size in bytes.

modified(path)

returns number

When the file last changed, as a timestamp (see the time library).

home()

returns text

Your home folder.

cwd()

returns text

The current folder (where relative paths start).

here(name?)

returns text

The folder your program is in, or the path of name inside it: fs.read(fs.here("data.txt")).

join(part, ...)

returns text

Join path parts with the right slash for this computer: join("logs", "auth.log").

name(path)

returns text

The last part of a path: name("logs/auth.log") is "auth.log".

ext(path)

returns text

The file extension with its dot: ext("auth.log") is ".log" ("" if none).

parent(path)

returns text

The folder that holds path: parent("logs/auth.log") is "logs".