Skip to content

io — files, stdin, environment ​

mzs
include io

WARNING

Not available in bot scripts: there are no files and no environment variables there.

In the terminal io is always there; mzs --no-io removes it, --read-only forbids writing.

FunctionWhat it does
io.read(path)the whole file as a string
io.write(path, s)write (create or overwrite) → number of bytes
io.append(path, s)append to the end (create if missing) → number of bytes
io.exists(path)whether the name exists
io.stat(path){size, dir} or nil
io.ls(dir = ".")names in the folder, alphabetically
io.walk(dir = ".")all paths under the folder, lazily; folders end with /
io.mkdir(path)create a folder along with its parents
io.mv(from, to)rename
io.rm(path)remove one name (a file or an empty folder) → whether it existed
io.stdinall of stdin as a string
io.linesstdin line by line, lazily (seq)
io.env(name, default = nil)an environment variable; an empty one also gives default

write and append count bytes, not characters. There are no open files (io.open), only whole strings.

Files ​

mzs
include io
io.write("notes.txt", "café\nbar\n")  # → 10
io.append("notes.txt", "baz\n")  # → 4
io.read("notes.txt").lines  # → ["café","bar","baz"]
io.exists("notes.txt")  # → true
io.stat("notes.txt")  # → {"size":14,"dir":false}
io.ls.filter { it.ends_with(".txt") }  # → ["notes.txt"]

JSON from disk and back:

mzs
include io
include json
io.write("config.json", {port: 8080, debug: false}.json)
cfg = json.parse(io.read("config.json"))
cfg["port"]  # → 8080

Folders ​

mzs
include io
include path
io.mkdir("src/lib")
io.write("src/a.mzs", "1")
io.write("src/lib/b.mzs", "2")
io.write("src/lib/c.txt", "3")
io.walk("src").array  # → ["src/a.mzs","src/lib/","src/lib/b.mzs","src/lib/c.txt"]
io.walk("src").filter { path.match("**/*.mzs", it) }.array  # → ["src/a.mzs","src/lib/b.mzs"]
io.mv("src/lib/c.txt", "src/c.txt")
io.ls("src")  # → ["a.mzs","c.txt","lib"]
[io.rm("src/c.txt"), io.rm("src/c.txt")]  # → [true,false]

rm is not recursive. Remove a tree by walking it from the end:

mzs
include io
io.mkdir("out/2026")
io.write("out/2026/a.txt", "x")
for p in io.walk("out").array.reverse { io.rm(p) }
io.rm("out")  # → true
io.exists("out")  # → false

stdin ​

mzs
include io
println(io.lines.count { it.has("ERROR") })
sh
mzs errors.mzs < app.log
  • io.stdin is all of the input as a string, read once and remembered.
  • io.lines goes line by line, with no limit on file size. It can be walked once; to walk it twice, use .array.
  • With no input, io.stdin is "" and io.lines is empty.
sh
printf 'one\ntwo\n' | mzs -e 'include io; io.lines.map { it.upper }.array'

Environment ​

mzs
include io
io.env("MZS_MODE", "dev")  # → "dev"
io.env("MZS_MISSING")  # → nil

Errors ​

Anything the disk refused is an io error with the file name. A missing file is not an error for exists and stat.

mzs
include io
try io.read("gone.txt") else (e) -> e["message"]  # → "io.read \"gone.txt\": open gone.txt: no such file or directory"
try io.read("gone.txt") else "(default)"  # → "(default)"
io.exists("gone.txt")  # → false