Skip to content

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
package main
import fmt
import file

var content, err = file.Read("test.txt")
if err != None {
    fmt.Println("Error:", err)
} else {
    fmt.Println(content)
}


Move(src, dest)

Renames (moves) a file from src to dest.

Parameters: - src: Source file path. - dest: Destination file path.

Returns: - (None, error)

Example:

Move File
1
2
3
4
package main
import file

var _, err = file.Move("/tmp/a.txt", "/tmp/b.txt")


Remove(path)

Deletes the named file.

Parameters: - path: File path to remove.

Returns: - (None, error)

Example:

Delete File
1
2
3
4
package main
import file

var _, err = file.Remove("/tmp/old.log")


Exists(path)

Reports whether a file or directory exists.

Parameters: - path: Path to check.

Returns: - (boolean, error)

Example:

Check File Exists
1
2
3
4
5
6
package main
import fmt
import file

var ok, err = file.Exists("/tmp/data.txt")
if err == None { fmt.Println(ok) }


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:

Touch File
1
2
3
4
package main
import file

var _, err = file.Touch("/tmp/heartbeat")


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
1
2
3
4
package main
import file

var _, err = file.Chmod("/path/to/file.txt", 420) // 0644


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
1
2
3
4
5
6
package main
import fmt
import file

var entries, err = file.ListRich("/tmp")
if err == None { fmt.Println(entries) }


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:

Write File
1
2
3
4
package main
import file

file.Write("test.txt", "Hello, Harneet!")


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:

Append File
1
2
3
4
package main
import file

file.Append("test.txt", "\nAnother line.")


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
1
2
3
4
package main
import file

var _, err = file.Copy("/source/file.txt", "/dest/file.txt")


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
1
2
3
4
5
6
7
8
package main
import fmt
import file

var lines, err = file.ReadLines("notes.txt")
if err == None {
    fmt.Println(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
1
2
3
4
5
6
package main
import file
import fmt

var _, err = file.WriteLines("notes.txt", ["line 1", "line 2", "line 3"])
if err != None { fmt.Println("write error:", err) }


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
1
2
3
4
5
6
package main
import file
import fmt

var p, err = file.TempFile("mytmp-*")
if err == None { fmt.Println("temp file:", p) }


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
1
2
3
4
5
6
7
package main
import file
import fmt

// All .ha files under ./examples, recursively, including hidden, ignoring vendor
var files, err = file.GetAllFiles("examples", true, "ha", true, ["vendor"])
if err == None { fmt.Println(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
1
2
3
4
5
6
7
8
import file
import fmt

var h, err = file.Open("log.txt", "a+")
if err == None {
    var _, _ = file.WriteTo(h, "hello\n")
    var _, _ = file.Close(h)
}


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:

Walk Directory Tree
import file
import fmt

var entries, err = file.Walk(".")
if err == None {
    for e in entries {
        var p, isDir, size, mt = e
        fmt.Println(p, isDir, size, mt)
    }
}