MojoScale Studio Docs
API Reference

filesystem

Internal flash filesystem backed by user SPIFFS.

Studio Docs Storage

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.

API 22 available

filesystem.write(path: str, data: any) -> bool

Write data to a file, replacing previous contents.

Parameters
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.
Returns

bool bool True when the write completes. Examples: filesystem.write("/config.txt", "mode=automatic")

filesystem.append(path: str, data: any) -> bool

Append data to a file, creating it if needed.

Parameters
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.
Returns

bool bool True when the append completes. Examples: filesystem.append("/events.log", "motor started\n")

filesystem.read(path: str) -> str

Read the entire contents of a text file.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes File path beginning with "/".
Returns

str str File contents. Examples: text = filesystem.read("/config.txt") print(text)

filesystem.read_bytes(path: str) -> bytes

Read the contents of a file as bytes.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes File path beginning with "/".
Returns

bytes bytes File contents.

filesystem.exists(path: str) -> bool

Return whether a file or directory exists.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes File or directory path beginning with "/".
Returns

bool bool True when the path exists.

filesystem.remove(path: str) -> bool

Delete a file.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes File path beginning with "/".
Returns

bool bool True when the file was removed.

filesystem.rename(old_path: str, new_path: str) -> bool

Rename or move a file.

Parameters
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.
Returns

bool bool True when the file was renamed.

filesystem.size(path: str) -> int

Return the size of a file in bytes.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes File path beginning with "/".
Returns

int int File size in bytes.

filesystem.mkdir(path: str) -> bool

Create a directory path.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes Directory path beginning with "/".
Returns

bool bool True when the directory exists or was created. Examples: filesystem.mkdir("/logs")

filesystem.rmdir(path: str) -> bool

Remove an empty directory path.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes Directory path beginning with "/".
Returns

bool bool True when the directory was removed.

filesystem.list(path: str = None) -> list

List direct child names inside a directory.

Parameters
Name Type Pass as Required Description
path str positional or keyword No Optional directory path. Defaults to "/".
Returns

list list Names of files and directories directly inside path. Examples: for entry in filesystem.list("/"): print(entry)

filesystem.info(path: str) -> dict

Return metadata about a file or directory.

Parameters
Name Type Pass as Required Description
path str positional or keyword Yes File or directory path beginning with "/".
Returns

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

Open a file for incremental access.

Parameters
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".
Returns

filesystem_file filesystem_file object.

filesystem.open().read(size=None) -> str

Read data from the current file position. Pass size to limit bytes read.

Parameters
Name Type Pass as Required Description
size any positional or keyword No Optional value. Defaults to None.

filesystem.open().readline() -> str

Read one line, or None at end of file.

filesystem.open().write(data) -> int

Write string or bytes data and return bytes written.

Parameters
Name Type Pass as Required Description
data any positional or keyword Yes Required value.

filesystem.open().flush() -> bool

Flush buffered data to storage.

filesystem.open().seek(offset) -> bool

Move to an absolute byte offset from the start of the file.

Parameters
Name Type Pass as Required Description
offset float positional or keyword Yes Required value.

filesystem.open().tell() -> int

Return the current byte position.

filesystem.open().close() -> bool

Flush and close the file.

filesystem.open().Examples

file = filesystem.open("/events.log", "a") file.write("sensor connected\n") file.flush() file.close()

filesystem.usage() -> dict

Return user_spiffs capacity and usage information.

Returns

dict dict Map with total, used, and free byte counts. Examples: storage = filesystem.usage() print(storage["free"])