← Python

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.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

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.

Learning
Duo
What does open() do?
Opens a file for reading or writingOpens a website
Matches
❌ f = open('data.txt') print(f.read()) f.write('hi')
Error: Can't write in read mode
Applying
Meanings
Term
open()
Meaning
Built-in function that opens a file.
Practice
with open('hello.txt', 'w') as f:
Testing
Gaps
with ('file.txt', 'r') as f:
Check Answer
Spots
1with open('file.txt') as f
2 content = f.read()

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Star Fox' badge — Frozen Master, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does open() do?
A. Opens a file for reading or writing ✓
B. Opens a website
open() connects your program to a file on disk — you then read or write through that connection
2. What does 'r' mode mean?
A. Run
B. Read only ✓
'r' is the default mode — it opens the file for reading and fails if file doesn't exist
3. What does 'w' mode do?
A. Writes overwrites existing content ✓
B. Reads the file
'w' creates the file if needed and wipes any existing content before writing
4. What does 'a' mode do?
A. Reads all
B. Appends adds to the end ✓
'a' keeps existing content and adds new content after it — safe for logs
5. Why use 'with open()'?
A. Automatically closes the file ✓
B. It's faster
with ensures the file closes even if an error occurs — no manual .close() needed

Match the Pairs

f = open('data.txt') print(f.read()) f.write('hi') Error: Can't write in read mode
open('missing.txt', 'r') Error: FileNotFoundError
with open('f.txt', 'r') as f: data = f.read() Valid: with auto-closes file
open('f.txt', 'a') Ok: Append mode — adds to end
with open('f.txt','w') as f: f.write('Hi') Output: (writes 'Hi' to file)
f = open('f.txt','r') print(type(f)) Output: <class '_io.TextIOWrapper'>

Key Term

open()
Built-in function that opens a file.~~'r' mode

Fill in the Blanks

1. with open('file.txt', 'r') as f:
2. with open('file.txt', 'r') as f:
3. with open('file.txt', 'w') as f:
4. with open('log.txt', 'a') as f: f.write('entry')
5. f = open('test.txt', 'w') f.write('Hi') f.close()

Spot the Error

Question 1
A with open('file.txt') as f
B content = f.read()
C print(content)
D print('Done')
Line A — Missing colon after the with statement
Question 2
A with Open('file.txt') as f:
B content = f.read()
C print(content)
D print('Done')
Line A — open is lowercase: Open is not valid
Question 3
A # Open without with block
B f = open('file.txt', 'r')
C content = f.read()
D # forgot to close
Line B — File is never closed: always close or use 'with'
Question 4
A with open('file.txt', 'r') as f:
B f.write('Hello')
C print('Written')
D print('Done')
Line B — Can't write in 'r' mode: use 'w' or 'a'
Question 5
A with open(file.txt, 'r') as f:
B content = f.read()
C print(content)
D print('Done')
Line A — Filename must be a string in quotes: 'file.txt'
For Teachers

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.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?