File Operations – Open

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

File Operations – Open

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does open() do?
A Opens a website
B Opens a file for reading or writing
2. What does 'r' mode mean?
A Read only
B Run
3. What does 'w' mode do?
A Writes overwrites existing content
B Reads the file
4. What does 'a' mode do?
A Appends adds to the end
B Reads all
5. Why use 'with open()'?
A It's faster
B Automatically closes the file

Match the Pairs

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

Left columnRight column
1f = open('data.txt') print(f.read()) f.write('hi') AOk: Append mode — adds to end
2open('missing.txt', 'r') BError: FileNotFoundError
3with open('f.txt', 'r') as f: data = f.read() CValid: with auto-closes file
4open('f.txt', 'a') DOutput: (writes 'Hi' to file)
5with open('f.txt','w') as f: f.write('Hi') EError: Can't write in read mode
6f = open('f.txt','r') print(type(f)) FOutput: <class '_io.TextIOWrapper'>

Definitions

open()

Fill in the Blanks

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

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

Spot the Error

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

Question 1
A with open('file.txt') as f
B content = f.read()
C print(content)
D print('Done')
Question 2
A with Open('file.txt') as f:
B content = f.read()
C print(content)
D print('Done')
Question 3
A # Open without with block
B f = open('file.txt', 'r')
C content = f.read()
D # forgot to close
Question 4
A with open('file.txt', 'r') as f:
B f.write('Hello')
C print('Written')
D print('Done')
Question 5
A with open(file.txt, 'r') as f:
B content = f.read()
C print(content)
D print('Done')

Answer Key

File Operations – Open
Section A: Quick Questions
Q1
B — Opens a file for reading or writing
Q2
A — Read only
Q3
A — Writes overwrites existing content
Q4
A — Appends adds to the end
Q5
B — Automatically closes the file
Section B: Match the Pairs
Q1
f = open('data.txt') print(f.read()) f.write('hi') → Error: Can't write in read mode
Q2
open('missing.txt', 'r') → Error: FileNotFoundError
Q3
with open('f.txt', 'r') as f: data = f.read() → Valid: with auto-closes file
Q4
open('f.txt', 'a') → Ok: Append mode — adds to end
Q5
with open('f.txt','w') as f: f.write('Hi') → Output: (writes 'Hi' to file)
Q6
f = open('f.txt','r') print(type(f)) → Output: <class '_io.TextIOWrapper'>
Section C: Definitions
Q1
open() — Built-in function that opens a file.~~'r' mode
Section D: Fill in the Blanks
Q1
with open('file.txt', 'r') as f:
Q2
with open('file.txt', 'r') as f:
Q3
with open('file.txt', 'w') as f:
Q4
with open('log.txt', 'a') as f: f.write('entry')
Q5
f = open('test.txt', 'w') f.write('Hi') f.close()
Section E: Spot the Error
Q1
A — Missing colon after the with statement
Q2
A — open is lowercase: Open is not valid
Q3
B — File is never closed: always close or use 'with'
Q4
B — Can't write in 'r' mode: use 'w' or 'a'
Q5
A — Filename must be a string in quotes: 'file.txt'