The filesystem component is OpenComputers' standard file and directory API. It provides labeled volumes, file metadata queries, directory operations, and handle-based file I/O.
Compared with the raw drive component, filesystem is the higher-level storage surface most Lua programs should use.
- Repository:
opencomputers - Component name:
filesystem - Typical hosts: hard drives, floppy disks, raid volumes, temporary filesystems, mounted storage
- Paths are sanitized internally. Attempts to escape the filesystem root with
..are rejected. "/"and"."are normalized to the filesystem root.- File handles belong to the computer that opened them. Using another machine's handle causes
"bad file descriptor". - Supported open modes are:
"r"/"rb""w"/"wb""a"/"ab"
local component = require("component")
local fs = component.filesystem
if not fs then
error("filesystem component not available")
endFor a specific disk, use component.proxy(address).
- Syntax:
fs.getLabel() - Returns:
stringornil - Purpose: Reads the volume label.
- Syntax:
fs.setLabel(value) - Returns:
string - Purpose: Changes the volume label and returns the resulting label.
Possible error from source: "drive does not support labeling".
- Syntax:
fs.isReadOnly() - Returns:
boolean - Purpose: Checks whether the filesystem is read-only.
- Syntax:
fs.spaceTotal() - Returns:
number - Purpose: Gets total filesystem capacity in bytes.
If the backend reports unlimited size, the source returns positive infinity.
- Syntax:
fs.spaceUsed() - Returns:
number - Purpose: Gets used filesystem capacity in bytes.
- Syntax:
fs.exists(path) - Returns:
boolean - Purpose: Checks whether a path exists.
- Syntax:
fs.size(path) - Returns:
number - Purpose: Gets the size of the file or directory entry at a path.
- Syntax:
fs.isDirectory(path) - Returns:
boolean - Purpose: Checks whether the path points to a directory.
- Syntax:
fs.lastModified(path) - Returns:
number - Purpose: Gets the real-world modification timestamp of a path.
- Syntax:
fs.list(path) - Returns:
tableornil - Purpose: Lists names inside a directory.
If the directory does not exist or cannot be listed by the backend, this returns nil.
Example:
local entries = fs.list("/")
if entries then
for name in entries do
print(name)
end
end- Syntax:
fs.makeDirectory(path) - Returns:
boolean - Purpose: Creates a directory and any missing parent directories.
Behavior:
- Returns
truewhen creation succeeds. - Returns
falsewhen the path already exists or creation fails.
- Syntax:
fs.remove(path) - Returns:
boolean - Purpose: Removes a file or recursively deletes a directory tree.
This component implementation descends into directories and deletes children before deleting the parent.
- Syntax:
fs.rename(from, to) - Returns:
boolean - Purpose: Renames or moves a path.
- Syntax:
fs.open(path[, mode]) - Returns:
userdata - Purpose: Opens a file handle.
Parameters:
path:string: path to open.mode:stringoptional: defaults to"r".
Behavior:
- Raises
"unsupported mode"for unknown modes. - Raises
"too many open handles"when the current machine exceeds the configured handle limit. - The returned handle is userdata; keep it and reuse it for later operations.
Example:
local handle = fs.open("/hello.txt", "w")
fs.write(handle, "hello\n")
fs.close(handle)- Syntax:
fs.close(handle) - Returns: no direct return values
- Purpose: Closes an open file handle.
Possible error from source: "bad file descriptor".
- Syntax:
fs.read(handle, count) - Returns:
stringornil - Purpose: Reads up to
countbytes from a file handle.
Parameters:
handle:userdatacount:number
Behavior:
- Returns
nilon EOF. - Read size is clamped to the configured maximum read buffer size.
- Possible errors include:
"bad file descriptor""not enough energy"
- Syntax:
fs.seek(handle, whence, offset) - Returns:
number - Purpose: Changes the file pointer and returns the new position.
Parameters:
handle:userdatawhence:string:"set","cur", or"end".offset:number
Unknown whence values raise "invalid mode".
- Syntax:
fs.write(handle, value) - Returns:
boolean - Purpose: Writes bytes to an open file handle.
Parameters:
handle:userdatavalue:string
Behavior:
- Returns
trueon success. - Possible errors include:
"bad file descriptor""not enough energy"
local handle = fs.open("/note.txt", "w")
fs.write(handle, "OpenComputers\n")
fs.close(handle)
handle = fs.open("/note.txt", "r")
print(fs.read(handle, 64))
fs.close(handle)drivedisk_drivecomponent