Skip to content

fs

This content is for v1.0. Switch to the latest version for up-to-date documentation.

The fs namespace provides synchronous file system operations. No import is required.


FunctionSignatureReturn TypeDescription
fs.readfs.read(path)stringReads an entire file as a UTF-8 string.
fs.writefs.write(path, content)voidWrites content to a file, overwriting existing data.
fs.appendfs.append(path, content)voidAppends content to the end of a file.
fs.existsfs.exists(path)boolChecks if a file or directory exists.
fs.mkdirfs.mkdir(path)boolCreates a directory (including parent directories).
fs.readdirfs.readdir(path)arrayLists file and folder names inside a directory.
fs.removefs.remove(path)boolDeletes a file or directory.
fs.copyfs.copy(src, dest)boolCopies a file from source to destination.

Reads the full content of the file specified by path. Throws an exception if the file cannot be opened or does not exist.

try {
var content = fs.read("config.json");
var config = json.parse(content);
print("Database Host: " + config["host"]);
} catch (err) {
print("Failed to read file: " + err);
}

Writes string content into the file at path. If the file does not exist, it will be created. If parent directories do not exist, ensure you create them with fs.mkdir.

var log_entry = "Session started at " + str(datetime.timestamp()) + "\n";
fs.write("logs/session.log", log_entry);

Appends string data to an existing file without modifying previously written content.

fs.append("events.csv", "user_signup,104,success\n");

Returns true if the specified file or folder exists on the disk, or false otherwise.

if (fs.exists("savegame.dat")) {
var save = fs.read("savegame.dat");
print("Loaded save data: " + str(len(save)) + " bytes");
} else {
print("No existing save file found. Initializing new game.");
}

Recursively creates all directories in the specified path. Returns true if the directory was created or already exists.

fs.mkdir("build/output/assets");

Scans the given directory and returns an array of strings representing the names of entries inside it.

var files = fs.readdir("./assets");
var i = 0;
while (i < len(files)) {
var filename = files[i];
print("Found asset: " + filename);
i = i + 1;
}

Removes a file or an empty directory. Returns true upon successful deletion.

if (fs.exists("temp.tmp")) {
fs.remove("temp.tmp");
}

Copies a file from src to dest.

fs.copy("assets/default_skin.png", "user_data/skin.png");