dogma:fs

import { read_file, write_file } from "dogma:fs"

The host must grant the dogma:fs capability for these functions to be available. See the Embedding Guide.

Reading files🔗

FunctionSignatureDescription
read_file(path: String) -> StringRead file as UTF-8 string
read_file_bytes(path: String) -> [i32]Read file as raw byte array

Writing files🔗

FunctionSignatureDescription
write_file(path: String, content: String)Write string to file (creates or overwrites)
append_file(path: String, content: String)Append string to file

Directory operations🔗

FunctionSignatureDescription
read_dir(path: String) -> [String]List directory contents (names only)
mkdir(path: String)Create directory and all parents
remove(path: String)Delete file or empty directory

Path utilities🔗

FunctionSignatureDescription
exists(path: String) -> boolReturn true if path exists
is_file(path: String) -> boolReturn true if path is a regular file
is_dir(path: String) -> boolReturn true if path is a directory
copy(src: String, dst: String)Copy file from src to dst
rename(src: String, dst: String)Move/rename file or directory

Example🔗

import { read_file, write_file, exists } from "dogma:fs"
import { println } from "dogma:core"

fn main() {
    if exists("output.txt") {
        let content = read_file("output.txt")
        println(f"Existing content: {content}")
    }
    write_file("output.txt", "Hello from Dogma!\n")
}