Skip to content
dreamcode
dreamcode
Back to map
File handling · Lesson 28 of 44
+15 XP on finish
PYTHON INTERMEDIATE

File handling

Use the with statement and open() function to open files. The context manager automatically closes the file when the block ends.

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.
main.py
PYTHON
real Python, runs in your browser
CONSOLE
- run your code to see output -