File Operations – Write

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

File Operations – Write

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does f.write('Hello') do?
A Writes 'Hello' to the file
B Prints 'Hello'
2. Does write() add a newline automatically?
A Yes always
B No you must add \n yourself
3. What does 'w' mode do to existing content?
A Erases it completely
B Keeps it
4. How do you write without erasing?
A Use 'a' mode (append)
B Use 'w' mode
5. f.write('Hi') returns?
A 'Hi'
B 2 (number of characters written)

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.write(42) AOk: Write list of strings
2with open('f.txt','w') as f: pass f.write('hi') BOutput: (file has 'AB')
3with open('f.txt','w') as f: f.write('Hello\n') CValid: Writes with newline
4lines = ['a\n','b\n'] f.writelines(lines) DError: File already closed
5with open('f.txt','w') as f: n = f.write('Hi') print(n) EOutput: 2
6with open('f.txt','w') as f: f.write('A') f.write('B') FError: write() needs a string

Definitions

.write(string)

Fill in the Blanks

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

1.
with open('f.txt', 'w') as f: f._____('Hello')
2.
with open('f.txt', ___) as f: f.write('Append this')
3.
f.write('Line 1__')
4.
score = 95 f.write('Score: ' + ___(score))
5.
lines = ['a\n', 'b\n'] f.__________(lines)

Spot the Error

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

Question 1
A score = 42
B with open('f.txt', 'w') as f:
C f.write(score)
D print('Saved!')
Question 2
A data = 'Hello World'
B with open('f.txt', 'r') as f:
C f.write(data)
D print('Written!')
Question 3
A with open('f.txt', 'w') as f:
B f.write('Line 1')
C f.write('Line 2')
Question 4
A lines = ['a', 'b', 'c']
B with open('f.txt', 'w') as f:
C f.writelines(lines)
D print('Written!')
Question 5
A f = open('f.txt', 'w')
B f.write('Hello')
C # no close

Answer Key

File Operations – Write
Section A: Quick Questions
Q1
A — Writes 'Hello' to the file
Q2
B — No you must add \n yourself
Q3
A — Erases it completely
Q4
A — Use 'a' mode (append)
Q5
B — 2 (number of characters written)
Section B: Match the Pairs
Q1
with open('f.txt','w') as f: f.write(42) → Error: write() needs a string
Q2
with open('f.txt','w') as f: pass f.write('hi') → Error: File already closed
Q3
with open('f.txt','w') as f: f.write('Hello\n') → Valid: Writes with newline
Q4
lines = ['a\n','b\n'] f.writelines(lines) → Ok: Write list of strings
Q5
with open('f.txt','w') as f: n = f.write('Hi') print(n) → Output: 2
Q6
with open('f.txt','w') as f: f.write('A') f.write('B') → Output: (file has 'AB')
Section C: Definitions
Q1
.write(string) — Writes a string to the file.~~.writelines(list)
Section D: Fill in the Blanks
Q1
with open('f.txt', 'w') as f: f.write('Hello')
Q2
with open('f.txt', 'a') as f: f.write('Append this')
Q3
f.write('Line 1\n')
Q4
score = 95 f.write('Score: ' + str(score))
Q5
lines = ['a\n', 'b\n'] f.writelines(lines)
Section E: Spot the Error
Q1
C — write() only accepts strings: convert number with str()
Q2
B — Can't write in 'r' mode: use 'w' or 'a'
Q3
C — No newlines both lines will be on the same line: 'Line 1Line 2'
Q4
C — writelines doesn't add \n: strings run together: 'abc'
Q5
A — File never closed data may not be saved. Use 'with'.