File Operations – Open
open() opens a file and returns a file object; the with statement closes it automatically when done
What it does: The with statement automatically closes the file when the block ends. 'a' mode opens the file and adds to the end without erasing existing content. open() connects your program to a file on disk — you then read or write through that connection. 'r' requires the file to exist — only 'w' and 'a' create files automatically. Unclosed files may not flush writes to disk and waste OS file handles. Opening a non-existent file in 'r' mode raises FileNotFoundError. Use try/except. 'w' mode creates or overwrites the file. f.write() writes the string. open() returns a file object. type() shows its class name.
Common mistakes: Missing colon after the with statement. open is lowercase: Open is not valid.
Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
open()Fill in the Blanks
Spot the Error
Create a free class, add this module to a journey, and track who's stuck.
Create a free teacher account →No credit card. No setup fee. Make a class and invite students in under three minutes.
Practice this topic interactively, track progress, and unlock badges.
Start Learning Free →