SolarOS

SolarOS 4.13.4 manual · api

Python storage and files API

API overview · Lua storage and files

Files and imports

open(path[, mode]) opens a file through SolarOS storage. Text mode is the default; add b for bytes. The supported base modes are r, w, a, and exclusive-create x, and each can use + for updating. File objects support read, readinto, readline, readlines, write, seek, tell, flush, and close, and can be used as context managers.

with open("/notes/example.txt", "w") as output:
    output.write("hello from Python\n")

with open("/notes/example.txt") as source:
    print(source.read())

Paths use the same preferred-storage and explicit-mount rules as solaros.storage. They cannot bypass SolarOS mounts to reach an unrelated host filesystem. Long reads and writes remain responsive to app cancellation.

External imports support .py, .mpy, and package directories containing __init__.py or __init__.mpy. A file-run starts its search in the script's directory and then searches the other entries in sys.path. The REPL and source-runner start relative imports at the preferred storage root.

/apps/weather/main.py
/apps/weather/sensors.py
/apps/weather/display/__init__.py

From main.py, both import sensors and import display resolve beside the script. Imported files pass through the same SolarOS path resolver as open().

print(solaros.storage.resolve("/.shell/history"))

solaros.storage

Storage functions expose SD mount and filesystem service operations.

Example:

import solaros

if not solaros.storage.is_mounted():
    solaros.storage.mount()

print(solaros.storage.usage("/"))
print(solaros.storage.read_file("/notes/example.txt", 512))
for block in solaros.storage.blocks():
    print(block["name"], block["type"], block["mounted"], block["mount_point"])

Quick reference

Use solaros.storage for storage and files. See man python for runtime conventions and service availability.

Join us on: