PYTHON INTERMEDIATEChapter 7 · Errors, Files and Modules
File handling
Use the with statement and open() function to open files. The context manager automatically closes the file when the block ends.
Worked example
How it reads
- open("sky.txt", "w") opens a file named sky.txt for writing
- with ... as f automatically closes the file object

Cloud tip: Always prefer with open(...) to manual file opening to prevent memory leaks.


