io

File I/O, directory utilities, stdin, and stdout/stderr sub-objects.

Import

import io

File operations

io.read_file(path: str) -> str

Read entire file as a string.

io.write_file(path: str, data: str)

Write string to file, replacing any existing content.

io.append_file(path: str, data: str)

Append string to file.

io.read_lines(path: str) -> [str]

Read file as an array of lines.

io.write_lines(path: str, lines: [str])

Write array of lines to file.

io.read_bytes(path: str) -> [int]

Read file as a byte array.

io.write_bytes(path: str, bytes: [int])

Write byte array to file.

io.read_json(path: str) -> any

Read file and parse as JSON.

io.write_json(path: str, val: any)

Serialize value to JSON and write to file.

File info

io.exists(path: str) -> bool

True if path exists. Also available as io.file_exists(path).

io.size(path: str) -> int

File size in bytes. Also available as io.file_size(path).

io.file_info(path: str) -> map

Map of file metadata (size, mtime, etc.).

io.is_file(path: str) -> bool

True if path is a regular file.

io.is_dir(path: str) -> bool

True if path is a directory.

File manipulation

io.delete_file(path: str)

Delete a file.

io.copy_file(src: str, dst: str)

Copy a file.

io.move_file(src: str, dst: str)

Move a file.

io.rename_file(old: str, new: str)

Rename a file.

Create a symbolic link.

Directories

io.make_dir(path: str)

Create a directory recursively.

io.list_dir(path: str) -> [str]

List directory entries.

io.glob(pattern: str) -> [str]

Expand a glob pattern.

io.temp_file() -> str

Create a temporary file and return its path. io.temp_dir() creates a temporary directory.

Stdin

io.read_line(prompt?: str) -> str

Read a line from stdin, with optional prompt.

io.stdin_read() -> str

Read all of stdin.

io.stdin_readline() -> str

Read one line from stdin.

io.stdin_read_n(n: int) -> str

Read n bytes from stdin.

io.stdin_lines() -> [str]

Read all stdin lines as an array.

stdout / stderr

io.stdout.write(s), io.stdout.writeln(s), io.stdout.flush()
io.stderr.write(s), io.stderr.writeln(s), io.stderr.flush()

Examples

import io

io.write_file("out.txt", "hello\n")
let text = io.read_file("out.txt")
println(text)                    -- hello

let lines = io.read_lines("data.txt")
for line in lines { println(line) }

println(io.exists("out.txt"))   -- true
println(io.is_file("out.txt"))  -- true
io.stderr.writeln("an error message")