Courses/Errors & Files

⚠️ Errors & Files

Reading & Writing Files

Use 'with open' for safe file I/O.

The with statement automatically closes the file when you're done — even if an error occurs.

with open("notes.txt", "w") as f:
    f.write("hello\n")

with open("notes.txt") as f:
    print(f.read())

The browser sandbox uses an in-memory filesystem — files don't persist across runs, but everything else works.

main.py
Output
Press Run to execute.
Expected output
py.run

Sign in to track your progress across lessons.