io

The io module reads and writes files, manages directories, streams large files through integer handles, manipulates paths, and watches the filesystem for changes. It is organized into the file, stream, path, and watch submodules, with flat top-level shortcuts for the most common operations. Import it with import io.

io.file

FunctionSignatureDescription
readio.file.read(path: string) → stringread the whole file as a string
writeio.file.write(path: string, content: string) → boolwrite content, truncating; true on success
appendio.file.append(path: string, content: string) → boolappend content, creating if needed
existsio.file.exists(path: string) → booltrue if the path exists
deleteio.file.delete(path: string) → boolremove the file; true on success
copyio.file.copy(src: string, dst: string) → boolcopy src to dst
renameio.file.rename(src: string, dst: string) → boolrename/move src to dst
sizeio.file.size(path: string) → intfile size in bytes; 0 on error
isFileio.file.isFile(path: string) → booltrue if the path is a regular file
isDirio.file.isDir(path: string) → booltrue if the path is a directory
mkdirio.file.mkdir(path: string) → boolcreate the directory and parents
readDirio.file.readDir(path: string) → string[]list entry names in the directory

io.stream

Stream functions operate on an integer handle returned by io.stream.open (or -1 on failure). Always close the handle when done. Open modes are "r", "w", "a", and "rw".

FunctionSignatureDescription
openio.stream.open(path: string, mode?: string) → intopen a file and return a handle (-1 on error); default mode "r"
readio.stream.read(id: int, n: int) → stringread up to n bytes
readLineio.stream.readLine(id: int) → stringread one line including the trailing newline
readAllio.stream.readAll(id: int) → stringread the rest of the stream
writeio.stream.write(id: int, data: string) → boolwrite data to the stream
seekio.stream.seek(id: int, offset: int) → boolseek to offset from the start
tellio.stream.tell(id: int) → intcurrent position; -1 on error
eofio.stream.eof(id: int) → booltrue if end of file reached
closeio.stream.close(id: int) → boolclose the handle

io.path

FunctionSignatureDescription
absio.path.abs(path: string) → stringabsolute form of the path
extio.path.ext(path: string) → stringfile extension, including the dot
baseio.path.base(path: string) → stringlast path element
dirio.path.dir(path: string) → stringdirectory portion of the path
joinio.path.join(parts: string[]) → stringjoin path elements
cleanio.path.clean(path: string) → stringnormalize the path
isAbsio.path.isAbs(path: string) → booltrue if the path is absolute
relio.path.rel(basePath: string, targetPath: string) → stringrelative path from base to target

io.watch

Watch the filesystem for changes (built on fsnotify). start returns an integer handle (-1 on error) and runs your callback for each event with an object {path, op}, where op is one of "create", "write", "remove", "rename", or "chmod". A watch covers a single path — walk a directory tree and add each subdirectory for recursive watching.

FunctionSignatureDescription
startio.watch.start(path: string, fn) → intwatch path; fn({path, op}) fires per event. Returns a handle (-1 on error)
addio.watch.add(handle: int, path: string) → booladd another path to an existing watcher
stopio.watch.stop(handle: int) → boolstop watching and free resources
import io

let h = io.watch.start("./config", fn(ev) {
    println(ev.op + ": " + ev.path)
})
// ... later ...
io.watch.stop(h)

Top-level shortcuts

FunctionSignatureDescription
readio.read(path: string) → stringread the whole file
writeio.write(path: string, content: string) → boolwrite content, truncating
appendio.append(path: string, content: string) → boolappend content
existsio.exists(path: string) → booltrue if the path exists
deleteio.delete(path: string) → boolremove the file
mkdirio.mkdir(path: string) → boolcreate the directory and parents
readDirio.readDir(path: string) → string[]list directory entries
isFileio.isFile(path: string) → booltrue if a regular file
isDirio.isDir(path: string) → booltrue if a directory
sizeio.size(path: string) → intfile size in bytes
copyio.copy(src: string, dst: string) → boolcopy a file
renameio.rename(src: string, dst: string) → boolrename/move a file
absio.abs(path: string) → stringabsolute path
extio.ext(path: string) → stringfile extension
baseio.base(path: string) → stringlast path element
dirio.dir(path: string) → stringdirectory portion
joinio.join(parts: string[]) → stringjoin path elements

Example

import io

// quick whole-file read/write
io.write("notes.txt", "hello\nworld\n")
print(io.read("notes.txt"))

// stream a large file line by line via a handle
let h = io.stream.open("notes.txt", "r")
while !io.stream.eof(h) {
    print(io.stream.readLine(h))
}
io.stream.close(h)
Standard library · View as Markdown · llms-full.txt