File Operations – Read

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

File Operations – Read

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. f.read() returns?
A One line
B The entire file as one string
2. f.readlines() returns?
A One string
B A list of all lines
3. f.readline() returns?
A The next single line
B All lines
4. Why use line.strip()?
A To remove the \n newline character
B To split the line
5. for line in f: what does this do?
A Loops through each line
B Reads the whole file

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1with open('f.txt','w') as f: f.read() AValid: Lines as a list
2with open('f.txt') as f: f.read() f.read() BOutput: Hi
3with open('f.txt') as f: lines = f.readlines() CError: Second read returns empty
4with open('f.txt') as f: for line in f: print(line.strip()) DOk: Loop line by line
5# file has 'Hello\nWorld' f.readline() EOutput: 'Hello\n'
6# file has 'Hi' print(f.read()) FError: Can't read in write mode

Definitions

.read()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
with open('f.txt') as f: content = f.____() print(content)
2.
with open('f.txt') as f: lines = f._________()
3.
with open('f.txt') as f: first = f.________()
4.
for line in f: print(line._____())
5.
with open('f.txt') as f: ___ line in f: print(line)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A with open('f.txt') as f:
B content = f.read
C print(content)
Question 2
A with open('f.txt') as f:
B lines = f.readlines
C print(lines)
Question 3
A with open('f.txt') as f:
B for line in f.readlines():
C print(line)
Question 4
A with open('f.txt') as f:
B content = f.read()
C content = f.read()
D print(content)
Question 5
A f = open('f.txt')
B for line in f:
C print(line)
D # forgot to close

Answer Key

File Operations – Read
Section A: Quick Questions
Q1
B — The entire file as one string
Q2
B — A list of all lines
Q3
A — The next single line
Q4
A — To remove the \n newline character
Q5
A — Loops through each line
Section B: Match the Pairs
Q1
with open('f.txt','w') as f: f.read() → Error: Can't read in write mode
Q2
with open('f.txt') as f: f.read() f.read() → Error: Second read returns empty
Q3
with open('f.txt') as f: lines = f.readlines() → Valid: Lines as a list
Q4
with open('f.txt') as f: for line in f: print(line.strip()) → Ok: Loop line by line
Q5
# file has 'Hello\nWorld' f.readline() → Output: 'Hello\n'
Q6
# file has 'Hi' print(f.read()) → Output: Hi
Section C: Definitions
Q1
.read() — Reads the entire file as one string.~~.readline()
Section D: Fill in the Blanks
Q1
with open('f.txt') as f: content = f.read() print(content)
Q2
with open('f.txt') as f: lines = f.readlines()
Q3
with open('f.txt') as f: first = f.readline()
Q4
for line in f: print(line.strip())
Q5
with open('f.txt') as f: for line in f: print(line)
Section E: Spot the Error
Q1
B — Missing parentheses read is a method, call it with ()
Q2
B — Missing parentheses readlines is a method
Q3
B — This works but is less efficient. Loop over f directly and use strip().
Q4
C — Second read() returns '' cursor is at end. Read once or use f.seek(0).
Q5
A — File never closed. Use 'with' to auto-close.