tos library / Getting started
Getting started
This page walks through using tos from scratch.
Installation
tos is a single file. Drop tos.py next to your script (or in the TrashOS root next to boot.py) and import it:
my-project/
├── tos.py
└── myscript.py
# myscript.py
import tos
print("Hello from tos", tos.version)
Writing a tosapp
Inside a TrashOS installation, apps live in tosapps/ and are launched by the shell when you type their filename (without .py) at the prompt. A minimal app that remembers a counter:
# tosapps/counter.py
import os, tos
data_dir = tos.appdata.path("counter")
state_file = os.path.join(data_dir, "count.txt")
count = 0
if os.path.exists(state_file):
with open(state_file) as f:
count = int(f.read() or 0)
count += 1
with open(state_file, "w") as f:
f.write(str(count))
print(f"Launched {count} time(s).")
Tip: Always go through tos.appdata.path() instead of writing to the current working directory — TrashOS may launch your app from any location.
Checking for root
import tos
if tos.is_root():
print("Dangerous operations unlocked")
else:
print("Re-run inside a 'root' shell to continue")