Reading and writing files

Open project files with open() and with; write temporary files under /tmp.

Runs in Zerethon IDE · Python 3.12 Official Python documentation ↗

A run sees the project's files in its working folder, read-only. A program may write files under /tmp; they are deleted when the run ends.

main.py
import csv
import json

with open("data.csv", newline="") as f:
    rows = list(csv.DictReader(f))
print([r["name"] for r in rows])

with open("/tmp/report.json", "w") as f:
    json.dump({"count": len(rows)}, f)

with open("/tmp/report.json") as f:
    print(json.load(f))
data.csv
name,score
ada,90
alan,85

Output

['ada', 'alan']
{'count': 2}
  • with open(...) as f: closes the file even when an error happens.
  • Paths are relative to the project root: open("data/input.txt") reads the file shown in the explorer as data/input.txt.
  • Writing next to your code (open("out.txt", "w")) fails with OSError: [Errno 30] Read-only file system — use /tmp/out.txt.