file Module
The file module provides functions for interacting with the file system.
Functions
Read(filename)
Reads the entire content of a file.
Parameters: - filename: The name of the file to read.
Returns: - (string, error): The content of the file.
Example:
| Read File | |
|---|---|
Move(src, dest)
Renames (moves) a file from src to dest.
Parameters: - src: Source file path. - dest: Destination file path.
Returns: - (None, error)
Example:
Remove(path)
Deletes the named file.
Parameters: - path: File path to remove.
Returns: - (None, error)
Example:
Exists(path)
Reports whether a file or directory exists.
Parameters: - path: Path to check.
Returns: - (boolean, error)
Example:
| Check File Exists | |
|---|---|
Touch(path)
Creates the file if it does not exist, or updates its modification time if it does.
Parameters: - path: File path to touch.
Returns: - (None, error)
Example:
Chmod(path, mode)
Changes the mode of the named file.
Parameters: - path: File path. - mode: Integer mode (e.g., 420 for 0644).
Returns: - (None, error)
Example:
| Change File Mode | |
|---|---|
ListRich(path)
Lists directory entries and returns an array of tuples (name string, isDir boolean, size int, modTime string) where modTime is RFC3339 UTC.
Parameters: - path: Directory path.
Returns: - (array, error)
Example:
| Read Directory | |
|---|---|
Write(filename, content)
Writes content to a file. If the file does not exist, it will be created. If it exists, its contents will be overwritten.
Parameters: - filename: The name of the file to write to. - content: The content to write.
Returns: - (None, error)
Example:
Append(filename, content)
Appends content to a file. If the file does not exist, it will be created.
Parameters: - filename: The name of the file to append to. - content: The content to append.
Returns: - (None, error)
Example:
Copy(src, dest)
Copies a file from a source path to a destination path.
Parameters: - src: The source file path. - dest: The destination file path.
Returns: - (None, error)
Example:
| Copy File | |
|---|---|
Size(filename)
Returns the size of a file in bytes.
Parameters: - filename: The name of the file.
Returns: - (integer, error): The size of the file in bytes.
ReadLines(filename)
Reads all lines from a text file and returns them as an array of strings.
Parameters: - filename: The file to read.
Returns: - (array, error)
Example:
| Read Lines | |
|---|---|
WriteLines(filename, lines)
Writes an array of strings to a text file, each string on a new line.
Parameters: - filename: The file to write to. - lines: Array of strings.
Returns: - (None, error)
Example:
| Write Lines | |
|---|---|
Create(path)
Creates an empty file at path (truncates if it exists).
Parameters: - path: File path
Returns: - (None, error)
IsFile(path)
Reports whether the given path refers to a regular file.
Parameters: - path: Path to check
Returns: - (boolean, error)
Delete(path)
Deletes the named file or an empty directory.
Parameters: - path: Path to delete
Returns: - (None, error)
TempFile(pattern)
Creates a new temporary file in the default temp directory and returns its path.
If pattern is empty, a default like harneet-* is used.
Parameters: - pattern: Name pattern for the temp file (may include *)
Returns: - (string, error)
Example:
| Get Temp File | |
|---|---|
TempDir(pattern)
Creates a new temporary directory in the default temp directory and returns its path.
If pattern is empty, a default like harneet-* is used.
Parameters: - pattern: Name pattern for the temp directory (may include *)
Returns: - (string, error)
CreateDirAll(path)
Creates a directory named path, along with any necessary parents (no-op if it already exists).
Parameters: - path: Directory path
Returns: - (None, error)
RemoveAll(path)
Removes path and any children it contains (files or directories).
Parameters: - path: Path to remove recursively
Returns: - (None, error)
IsEmpty(path)
Checks if a directory at path is empty.
Parameters: - path: Directory path
Returns: - (boolean, error)
GetAllFiles(path, recursive, fileType, getHidden[, ignoreDirectories])
Gets all files inside a directory with optional recursion and filtering.
Parameters: - path: Directory to scan - recursive: If true, walk subdirectories - fileType: Extension(s) to include. Accepts "txt", ".txt", or comma-separated like "txt,log". Use "" or "*" for all. - getHidden: If true, include dotfiles - ignoreDirectories (optional): Array of directory names or full paths to skip
Returns: - (array, error) — Array of file paths
Example:
| Get All Files | |
|---|---|
Open(path, mode)
Opens a file and returns a handle object for granular operations.
Supported modes: "r", "w", "a", "c" (create-only, fail if exists), "rw"/"r+", "w+", "a+".
Parameters: - path: File path - mode: Open mode
Returns: - (object, error) — file handle object
Example:
| Open File | |
|---|---|
Close(handle)
Closes an open file handle.
Parameters: - handle: File handle returned by file.Open
Returns: - (None, error)
ReadFrom(handle, n)
Reads up to n bytes from a file handle and returns a string. If n < 0, reads all remaining bytes from the current offset.
Parameters: - handle: File handle - n: Number of bytes to read (use -1 for all remaining)
Returns: - (string, error)
WriteTo(handle, data)
Writes the provided string to a file handle and returns the number of bytes written.
Parameters: - handle: File handle - data: String to write
Returns: - (int, error)
Seek(handle, offset, whence)
Sets the offset for the next read or write on the file handle and returns the new offset.
whence: 0 (start), 1 (current), 2 (end)
Parameters: - handle: File handle - offset: Byte offset - whence: Seek reference point (0/1/2)
Returns: - (int, error) — New offset
Walk(root)
Traverses a directory tree rooted at root and returns an array of tuples (path string, isDir boolean, size int, modTime string) where modTime is RFC3339 UTC.
Parameters: - root: Root directory path to walk
Returns: - (array, error)
Example: