import filesystem
The filesystem module reads, writes, manages, and inspects files stored in the device's internal user_spiffs partition. Use it for small application-owned files such as configuration, calibration values, short logs, cached responses, small JSON/text documents, and persistent application state. For removable or high-volume storage, use the sdcard module instead. Internal flash has finite write endurance, so avoid rewriting files at high frequency. Paths should start with "/". Directories are lightweight filesystem paths used to organize files.
Quick example
import filesystem
filesystem.write("/hello.txt", "Hello MojoScale")
print(filesystem.read("/hello.txt"))
filesystem.mkdir("/logs")
filesystem.append("/logs/events.txt", "device started\n")
print(filesystem.list("/logs"))
file = filesystem.open("/logs/events.txt", "r")
print(file.readline())
file.close()
storage = filesystem.usage()
print(storage["free"])
importfilesystem
Internal flash filesystem backed by user SPIFFS.
The filesystem module reads, writes, manages, and inspects files stored in the device's internal user_spiffs partition. Use it for small application-owned files such as configuration, calibration values, short logs, cached responses, small JSON/text documents, and persistent application state. For removable or high-volume storage, use the sdcard module instead. Internal flash has finite write endurance, so avoid rewriting files at high frequency. Paths should start with "/". Directories are lightweight filesystem paths used to organize files.
filesystem.write(path: str, data: any) -> bool
filesystem.write(path: str, data: any) -> boolWrite data to a file, replacing previous contents.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
data |
any |
positional or keyword | Yes | String or bytes to write. |
bool bool True when the write completes. Examples: filesystem.write("/config.txt", "mode=automatic")
filesystem.append(path: str, data: any) -> bool
filesystem.append(path: str, data: any) -> boolAppend data to a file, creating it if needed.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
data |
any |
positional or keyword | Yes | String or bytes to append. |
bool bool True when the append completes. Examples: filesystem.append("/events.log", "motor started\n")
filesystem.read(path: str) -> str
filesystem.read(path: str) -> strRead the entire contents of a text file.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
str str File contents. Examples: text = filesystem.read("/config.txt") print(text)
filesystem.read_bytes(path: str) -> bytes
filesystem.read_bytes(path: str) -> bytesRead the contents of a file as bytes.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
bytes bytes File contents.
filesystem.exists(path: str) -> bool
filesystem.exists(path: str) -> boolReturn whether a file or directory exists.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File or directory path beginning with "/". |
bool bool True when the path exists.
filesystem.remove(path: str) -> bool
filesystem.remove(path: str) -> boolDelete a file.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
bool bool True when the file was removed.
filesystem.rename(old_path: str, new_path: str) -> bool
filesystem.rename(old_path: str, new_path: str) -> boolRename or move a file.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
old_path |
str |
positional or keyword | Yes | Existing file path. |
new_path |
str |
positional or keyword | Yes | New file path. The destination directory must already exist. |
bool bool True when the file was renamed.
filesystem.size(path: str) -> int
filesystem.size(path: str) -> intReturn the size of a file in bytes.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
int int File size in bytes.
filesystem.mkdir(path: str) -> bool
filesystem.mkdir(path: str) -> boolCreate a directory path.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Directory path beginning with "/". |
bool bool True when the directory exists or was created. Examples: filesystem.mkdir("/logs")
filesystem.rmdir(path: str) -> bool
filesystem.rmdir(path: str) -> boolRemove an empty directory path.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | Directory path beginning with "/". |
bool bool True when the directory was removed.
filesystem.list(path: str = None) -> list
filesystem.list(path: str = None) -> listList direct child names inside a directory.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | No | Optional directory path. Defaults to "/". |
list list Names of files and directories directly inside path. Examples: for entry in filesystem.list("/"): print(entry)
filesystem.info(path: str) -> dict
filesystem.info(path: str) -> dictReturn metadata about a file or directory.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File or directory path beginning with "/". |
dict dict Map with name, path, type, and size. Examples: details = filesystem.info("/config.json") print(details["size"])
filesystem.open(path: str, mode: str = None) -> filesystem_file
filesystem.open(path: str, mode: str = None) -> filesystem_fileOpen a file for incremental access.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
path |
str |
positional or keyword | Yes | File path beginning with "/". |
mode |
str |
positional or keyword | No | Optional file mode. Supported values are "r", "w", "a", "rb", "wb", and "ab". Defaults to "r". |
filesystem_file filesystem_file object.
filesystem.open().read(size=None) -> str
filesystem.open().read(size=None) -> strRead data from the current file position. Pass size to limit bytes read.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
size |
any |
positional or keyword | No | Optional value. Defaults to None. |
filesystem.open().readline() -> str
filesystem.open().readline() -> strRead one line, or None at end of file.
filesystem.open().write(data) -> int
filesystem.open().write(data) -> intWrite string or bytes data and return bytes written.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
data |
any |
positional or keyword | Yes | Required value. |
filesystem.open().flush() -> bool
filesystem.open().flush() -> boolFlush buffered data to storage.
filesystem.open().seek(offset) -> bool
filesystem.open().seek(offset) -> boolMove to an absolute byte offset from the start of the file.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
offset |
float |
positional or keyword | Yes | Required value. |
filesystem.open().tell() -> int
filesystem.open().tell() -> intReturn the current byte position.
filesystem.open().close() -> bool
filesystem.open().close() -> boolFlush and close the file.
filesystem.open().Examples
filesystem.open().Examplesfile = filesystem.open("/events.log", "a") file.write("sensor connected\n") file.flush() file.close()
filesystem.usage() -> dict
filesystem.usage() -> dictReturn user_spiffs capacity and usage information.
dict dict Map with total, used, and free byte counts. Examples: storage = filesystem.usage() print(storage["free"])