API Filesystem
Filesystem access to the SD card (FAT32).
picocalc.fs
Section titled “picocalc.fs”Functions
Section titled “Functions”picocalc.fs.open(path [, mode])
Section titled “picocalc.fs.open(path [, mode])”Opens a file on the SD card.
- Parameters:
path(string): Absolute file path (e.g.,"/apps/hello/data.txt")mode(string, optional): File mode ("r","w","a","rb","wb", etc.). Defaults to"r". A mode containingrbut nowora(including"r+") opens the file read-only.
- Returns: a file handle (userdata), or
nil, err("permission denied","cannot open file","too many open files")
Handles have methods — f:read(n), f:write(s), f:seek(offset [, whence]), f:tell(),
f:close() — identical to the picocalc.fs.* functions. A handle closes
itself when garbage-collected, and local f <close> = picocalc.fs.open(...)
closes it at the end of the scope. Using a closed handle raises “attempt to
use a closed file”; close is idempotent. At most 16 files can be open at
once (FatFS), and files still open when the app exits are closed by the OS.
local f = picocalc.fs.open("/data/save.txt", "w")if f then picocalc.fs.write(f, "Hello") picocalc.fs.close(f)endpicocalc.fs.read(file, length)
Section titled “picocalc.fs.read(file, length)”Reads bytes from an open file.
- Parameters:
file(userdata): File handle fromopen()length(number): Number of bytes to read
- Returns: (string or nil) Data read, or
nilon error
Raises on a negative length or a closed handle; the length is clamped to the bytes left in the file.
local data = picocalc.fs.read(f, 1024)picocalc.fs.write(file, data)
Section titled “picocalc.fs.write(file, data)”Writes data to an open file.
- Parameters:
file(userdata): File handle fromopen()data(string): Data to write
- Returns: (number) Number of bytes written
local n = picocalc.fs.write(f, "content")picocalc.fs.close(file)
Section titled “picocalc.fs.close(file)”Closes an open file.
- Parameters:
file(userdata): File handle fromopen()
- Returns: None
picocalc.fs.exists(path)
Section titled “picocalc.fs.exists(path)”Checks if a file or directory exists.
- Parameters:
path(string): Absolute path
- Returns: (boolean)
trueif exists,falseotherwise
if picocalc.fs.exists("/data/save.txt") then -- Load saved dataendpicocalc.fs.readFile(path)
Section titled “picocalc.fs.readFile(path)”Reads an entire file into memory in one call.
- Parameters:
path(string): Absolute file path
- Returns: (string or nil) File contents, or
nilwhen the path is denied, missing or unreadable. A file larger than free memory raises a memory error instead of returningnil; wrap it inpcallwhen the size is not known to fit.
local content = picocalc.fs.readFile("/apps/hello/config.txt")picocalc.fs.size(path)
Section titled “picocalc.fs.size(path)”Returns the size of a file in bytes.
- Parameters:
path(string): Absolute file path
- Returns: (number) File size in bytes, or
-1on error (file not found or sandbox violation)
local bytes = picocalc.fs.size("/data/save.txt")if bytes >= 0 then print("File is " .. bytes .. " bytes")endpicocalc.fs.isDir(path)
Section titled “picocalc.fs.isDir(path)”Whether a path exists and is a directory.
- Parameters:
path(string): Absolute path
- Returns: (boolean)
trueonly when the path exists and is a directory (the volume root/is one, for an app with theroot-filesystemrequirement;fs.exists("/")andfs.stat("/")agree, the latter withsize0 and no timestamp).falsefor a file, for a missing path, and for a path the sandbox refuses — the same “not usable” answerfs.existsgives. - Errors: none
if picocalc.fs.isDir("/apps/mygame/assets") then for _, entry in ipairs(picocalc.fs.listDir("/apps/mygame/assets")) do print(entry.name, entry.size) endendpicocalc.fs.listDir(path)
Section titled “picocalc.fs.listDir(path)”Lists the contents of a directory.
- Parameters:
path(string): Absolute directory path
- Returns: (table) Array of entries, where each entry is a table with:
name(string): File or directory nameis_dir(boolean):trueif directory,falseif filesize(number): File size in bytes (0 for directories)
local entries = picocalc.fs.listDir("/apps")for _, e in ipairs(entries) do print(e.name, e.is_dir, e.size)endpicocalc.fs.mkdir(path)
Section titled “picocalc.fs.mkdir(path)”Creates a directory at the specified path.
- Parameters:
path(string): Absolute directory path to create
- Returns: (boolean)
trueif successful or directory already exists,falseon error
-- Create app data directorylocal data_dir = "/data/" .. APP_IDif picocalc.fs.mkdir(data_dir) then print("Data directory ready")endpicocalc.fs.seek(file, offset [, whence])
Section titled “picocalc.fs.seek(file, offset [, whence])”Moves the read/write position within an open file.
- Parameters:
file(userdata): File handle fromopen()offset(number): Byte offset, relative towhencewhence(string, optional):"set"(default) counts from the start of the file andoffsetmust be>= 0;"cur"counts from the current position;"end"counts from the end of the file."cur"and"end"accept negative offsets.
- Returns: (boolean)
trueon success,falseif the seek failed. Usetellto read the new position. - Errors: raises on an unknown
whence, a negativeoffsetwith"set", and a resulting position that is before the start of the file or beyond 2^31-1. A rejected seek leaves the position alone. A position past the end of the file is left to the SD driver: on the device a read-only handle stops at the end of the file (tellreports the file length), while a writable handle grows the file at seek time (the gap holds undefined data) andtellreports the requested position. The simulator differs: it does not clamp or extend on a seek, only on a later write, so keep seeks within the file if the result matters.
picocalc.fs.seek(f, 0) -- Seek to beginningpicocalc.fs.seek(f, 0, "end") -- Seek to the end...local length = picocalc.fs.tell(f) -- ...so tell() is the file lengthpicocalc.fs.seek(f, -4, "cur") -- Back up four bytespicocalc.fs.tell(file)
Section titled “picocalc.fs.tell(file)”Returns the current byte position within an open file.
- Parameters:
file(userdata): File handle fromopen()
- Returns: (number) Current byte offset
local pos = picocalc.fs.tell(f)file:fsize()
Section titled “file:fsize()”The total size of an open file in bytes, without moving the read position.
- Returns: (number or nil, string) Size in bytes, or
nilplus an error message if it could not be read. Callable asf:fsize()orpicocalc.fs.fsize(f). - Errors: raises if the handle is closed
seek(f, 0, "end") followed by tell(f) gives the same number but leaves the
handle at end of file, so it changes where the next read starts. This does not:
local f = picocalc.fs.open(picocalc.fs.appPath("level.dat"), "rb")local total = f:fsize() -- or picocalc.fs.fsize(f); position unchangedlocal chunk = f:read(16) -- still the first 16 bytespicocalc.fs.appPath(name)
Section titled “picocalc.fs.appPath(name)”Returns the path /data/<appname>/<name>, automatically creating the app’s data directory if it does not exist. This is the recommended way to access per-app persistent storage.
- Parameters:
name(string): Filename within the app’s data directory
- Returns: (string) Full path (e.g.,
"/data/myapp/save.json")
local save_path = picocalc.fs.appPath("save.json")local f = picocalc.fs.open(save_path, "w")picocalc.fs.write(f, '{"score": 100}')picocalc.fs.close(f)picocalc.fs.browse([startDir])
Section titled “picocalc.fs.browse([startDir])”Opens a file-browser overlay panel. The user can navigate directories and select a file; Esc goes up a directory (and cancels at the top), and the system menu key cancels from any directory. It is system UI: it draws in the built-in 6x8 font and your font is restored.
- Parameters:
startDir(string, optional): Starting directory. Defaults to the app’s/data/<appname>/directory.
- Returns: (string or nil) Selected file path, or
nilif cancelled
local selected = picocalc.fs.browse("/apps")if selected then print("Selected: " .. selected)endpicocalc.fs.copy(src, dst)
Section titled “picocalc.fs.copy(src, dst)”Copy a file. Subject to filesystem sandbox.
- Parameters:
src(string): Source pathdst(string): Destination path
- Returns: (boolean)
trueon success;(false, string)on failure
local ok, err = picocalc.fs.copy("/data/myapp/save.json", "/data/myapp/save_backup.json")if not ok then print("Copy failed: " .. err) endpicocalc.fs.delete(path)
Section titled “picocalc.fs.delete(path)”Delete a file. Subject to filesystem sandbox.
- Parameters:
path(string): File path to delete
- Returns: (boolean)
trueon success;(false, string)on failure
local ok, err = picocalc.fs.delete("/data/myapp/old_save.json")if not ok then print("Delete failed: " .. err) endpicocalc.fs.deleteRecursive(path)
Section titled “picocalc.fs.deleteRecursive(path)”Delete a directory and all its contents. Subject to filesystem sandbox.
- Parameters:
path(string): Directory path to delete
- Returns: (boolean)
trueon success;(false, string)on failure
local ok, err = picocalc.fs.deleteRecursive("/data/myapp/cache")if not ok then print("Delete failed: " .. err) endpicocalc.fs.rename(src, dst)
Section titled “picocalc.fs.rename(src, dst)”Rename or move a file. Both paths subject to sandbox.
- Parameters:
src(string): Current pathdst(string): New path
- Returns: (boolean)
trueon success;(false, string)on failure
local ok, err = picocalc.fs.rename("/data/myapp/temp.txt", "/data/myapp/final.txt")if not ok then print("Rename failed: " .. err) endpicocalc.fs.stat(path)
Section titled “picocalc.fs.stat(path)”Get file or directory information. Subject to sandbox.
- Parameters:
path(string): File or directory path
- Returns: (table)
{size = number, is_dir = boolean}, or(nil, string)on failure
local info, err = picocalc.fs.stat("/data/myapp/save.json")if info then print("Size: " .. info.size .. ", is_dir: " .. tostring(info.is_dir))endpicocalc.fs.diskInfo()
Section titled “picocalc.fs.diskInfo()”Get SD card disk space information.
- Parameters: None
- Returns: (table)
{free = number, total = number}(values in KB), or(nil, string)on failure
local info, err = picocalc.fs.diskInfo()if info then print("Free: " .. info.free .. " KB / Total: " .. info.total .. " KB")endpicocalc.fs.glob(path, pattern)
Section titled “picocalc.fs.glob(path, pattern)”List files in a directory matching a glob pattern. Subject to sandbox.
- Parameters:
path(string): Directory to searchpattern(string): Glob pattern (e.g."*.lua")
- Returns: (table) Array of matching filenames
local lua_files = picocalc.fs.glob("/apps/myapp", "*.lua")for _, name in ipairs(lua_files) do print(name)endpicocalc.fs.ensureReady()
Section titled “picocalc.fs.ensureReady()”Ensure the SD card is mounted and ready.
- Parameters: None
- Returns: (boolean)
trueif SD card is ready
if picocalc.fs.ensureReady() then -- Safe to perform file operationsendpicocalc.fs.setSlowMode(enabled)
Section titled “picocalc.fs.setSlowMode(enabled)”Enable or disable slow SD card mode. Slow mode reduces SPI clock speed for compatibility with some SD cards.
- Parameters:
enabled(boolean):trueto enable slow mode,falseto disable
- Returns: None
picocalc.fs.setSlowMode(true) -- Use slower SPI clock for compatibilityDifferences from the native C API
Section titled “Differences from the native C API”The native g_api.fs table (picocalc_fs_t) and this Lua table overlap but are not identical. Porting a native app, or reading the C header:
Native C (api->fs->...) | Lua | Note |
|---|---|---|
fsize(file) | picocalc.fs.fsize(f) / f:fsize(), or picocalc.fs.size(path) for a path | Same. seek(f, 0, "end") + tell(f) also works but moves the position |
isDir(path) | picocalc.fs.isDir(path) | Same. stat(p).is_dir also works but needs st and st.is_dir, since stat returns nil for a missing path |
seek(file, offset) (absolute, bool) | picocalc.fs.seek(f, offset) | Same default; Lua adds whence ("set", "cur", "end"); C has absolute seeks only |
tell(file) | picocalc.fs.tell(f) | Same |
read(file, buf, len) (fills a buffer, returns a count) | picocalc.fs.read(f, len) (returns a string, nil at end of file) | |
open(path, mode) (NULL on error) | picocalc.fs.open(path [, mode]) (nil, err) | Lua handles are objects with methods and close on garbage collection |
Lua-only additions: readFile, appPath, copy, deleteRecursive, stat, diskInfo, glob, ensureReady and setSlowMode. Names differ for two functions: C deleteFile is picocalc.fs.delete, and C renameFile is picocalc.fs.rename; C listDir takes a callback where Lua returns a table.