This module contains functions for interacting with the file system and path operations natively in Rust.

Include using

var fs = use("std:fs");

read_file

read_file(path)

Reads the contents of a file and returns it as a string. Throws ValueError if the file cannot be read.

var text = fs.read_file("hello.txt");

write_file

write_file(path, content = "")

Writes text to a file. Overwrites existing content or creates a new file.

fs.write_file("hello.txt", "Hello, world!");

append_file

append_file(path, content)

Appends text data to the end of a file without overwriting existing contents.

fs.append_file("log.txt", "New log entry\n");

read_bytes

read_bytes(path)

Reads raw binary data from a file and returns a list of byte numbers (0-255).

var bytes = fs.read_bytes("image.png");

write_bytes

write_bytes(path, bytes_list)

Writes a list of raw byte numbers (0-255) to a binary file.

fs.write_bytes("output.bin", [72, 101, 108, 108, 111]);

exists

exists(path)

Checks whether a file or directory exists. Returns true or false.


is_file / is_dir / is_absolute

is_file(path)
is_dir(path)
is_absolute(path)
  • is_file: Returns true if path exists and points to a regular file.
  • is_dir: Returns true if path exists and points to a directory.
  • is_absolute: Returns true if path is absolute.
if (fs.is_file("main.kyro")) {
    echo "It's a file!";
}

copy / rename / remove_file

copy(src, dest)
rename(old_path, new_path)
remove_file(path)
  • copy: Copies a file from src to dest.
  • rename: Renames or moves a file/directory.
  • remove_file: Deletes a file from disk.

create_dir / remove_dir / read_dir

create_dir(path, recursive = false)
remove_dir(path, recursive = false)
read_dir(path)
  • create_dir: Creates a directory (optionally recursive).
  • remove_dir: Removes a directory.
  • read_dir: Returns a list of filename strings inside the directory.
var files = fs.read_dir("src");
for (var f in files) {
    echo f;
}

file_size / modified_time / metadata

file_size(path)
modified_time(path)
metadata(path)
  • file_size: Returns file size in bytes as a number.
  • modified_time: Returns last modified Unix epoch timestamp in seconds.
  • metadata: Returns a dictionary of file system metadata (size, is_file, is_dir, is_symlink, readonly, created, modified, accessed).
var meta = fs.metadata("main.kyro");
echo meta["size"];     // Bytes
echo meta["is_file"];  // true

parent_dir / filename / extension / canonicalize

parent_dir(path)
filename(path)
extension(path)
canonicalize(path)
  • parent_dir: Extracts parent directory path (/a/b/c.txt $\rightarrow$ /a/b).
  • filename: Extracts filename (/a/b/c.txt $\rightarrow$ c.txt).
  • extension: Extracts file extension (/a/b/c.txt $\rightarrow$ .txt).
  • canonicalize: Resolves relative components (. and ..) to return an absolute path.

temp_dir / temp_file

temp_dir()
temp_file(prefix = "kyro_tmp_")
  • temp_dir: Returns the OS system temporary directory path (/tmp or %TEMP%).
  • temp_file: Creates a unique empty temporary file and returns its absolute path string.