Files

Read and write /uploads, including the sample notes.txt.

Attach notes.txt with Add files, then read it. Writes go to /uploads/ and show up as download chips.

Goal

List uploads, read notes.txt, and write a small output file.

See uploads

import os

print(os.listdir("/uploads"))

If the list is empty, download notes.txt from the banner and attach it.

Read text

print(open("notes.txt").read())
print("---")
with open("notes.txt") as f:
    for line in f:
        print(line.strip())

with closes the file. open("notes.txt") and open("/uploads/notes.txt") both work after attach.

Write

import os

lines = ["Ada,98", "Alan,91", "Grace,95"]
with open("out.csv", "w") as f:
    f.write("name,score\n")
    for line in lines:
        f.write(line + "\n")
print("wrote out.csv")
print(os.listdir("/uploads"))
print(open("out.csv").read())

After Run, click on the file chip.

JSON

import json

payload = {"city": "Nairobi", "units": 12}
with open("payload.json", "w") as f:
    json.dump(payload, f)
print(json.loads(open("payload.json").read()))
Pitfall

Writing outside /uploads (for example a random home path) is blocked. Use a filename only: open("report.txt", "w").