← Python

File Operations – Read

.read() reads the entire file content as a string; .readlines() reads lines into a list

What it does: readlines() returns a list where each element is one line of the file. Iterating over the file object reads one line at a time. .strip() removes the \n. read() loads everything at once — fine for small files, memory-hungry for large ones. readline() advances the cursor one line — call it again to get the next. Processing line by line keeps only one line in memory at a time. The file cursor stays where reading stopped — seek(0) resets it to the start. split('\n') breaks the big string wherever there's a newline character. readlines() preserves the \n — use line.strip() or line.rstrip() to remove it.

Common mistakes: Missing parentheses read is a method, call it with (). Missing parentheses readlines is a method.

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
f.read() returns?
The entire file as one stringOne line
Matches
❌ with open('f.txt','w') as f: f.read()
Error: Can't read in write mode
Applying
Meanings
Term
.read()
Meaning
Reads the entire file as one string.
Practice
with open('hello.txt', 'r') as f:
Testing
Gaps
content = f.()
Check Answer
Spots
1with open('f.txt') as f:
2 content = f.read

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

The 'Desert Jerboa' badge — High Jumper, 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. f.read() returns?
A. The entire file as one string ✓
B. One line
read() loads everything at once — fine for small files, memory-hungry for large ones
2. f.readlines() returns?
A. One string
B. A list of all lines ✓
readlines() gives you a list — each element is one line including its \n
3. f.readline() returns?
A. The next single line ✓
B. All lines
readline() advances the cursor one line — call it again to get the next
4. Why use line.strip()?
A. To split the line
B. To remove the \n newline character ✓
Each line from a file ends with \n — strip() removes that trailing whitespace
5. for line in f: what does this do?
A. Loops through each line ✓
B. Reads the whole file
Iterating the file object yields one line per loop — memory-efficient

Match the Pairs

with open('f.txt','w') as f: f.read() Error: Can't read in write mode
with open('f.txt') as f: f.read() f.read() Error: Second read returns empty
with open('f.txt') as f: lines = f.readlines() Valid: Lines as a list
with open('f.txt') as f: for line in f: print(line.strip()) Ok: Loop line by line
# file has 'Hello\nWorld' f.readline() Output: 'Hello\n'
# file has 'Hi' print(f.read()) Output: Hi

Key Term

.read()
Reads the entire file as one string.~~.readline()

Fill in the Blanks

1. with open('f.txt') as f: content = f.read() print(content)
2. with open('f.txt') as f: lines = f.readlines()
3. with open('f.txt') as f: first = f.readline()
4. for line in f: print(line.strip())
5. with open('f.txt') as f: for line in f: print(line)

Spot the Error

Question 1
A with open('f.txt') as f:
B content = f.read
C print(content)
Line B — Missing parentheses read is a method, call it with ()
Question 2
A with open('f.txt') as f:
B lines = f.readlines
C print(lines)
Line B — Missing parentheses readlines is a method
Question 3
A with open('f.txt') as f:
B for line in f.readlines():
C print(line)
Line B — This works but is less efficient. Loop over f directly and use strip().
Question 4
A with open('f.txt') as f:
B content = f.read()
C content = f.read()
D print(content)
Line C — Second read() returns '' cursor is at end. Read once or use f.seek(0).
Question 5
A f = open('f.txt')
B for line in f:
C print(line)
D # forgot to close
Line A — File never closed. Use 'with' to auto-close.
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?