← Python

File Operations – Write

.write() writes a string to a file opened in write or append mode

What it does: \n adds a newline so the next write starts on a new line. writelines() writes each string from the list — doesn't add newlines automatically. write() sends text to the file — print() sends it to the screen. Unlike print(), write() is literal — add '\n' to end each line. 'w' truncates the file to zero length before writing — use 'a' to preserve content. 'a' (append) keeps existing content and adds new text after it. write() returns the count of characters written — 'Hi' has 2 characters. Each write() call appends to the file — include '\n' to separate lines.

Common mistakes: write() only accepts strings: convert number with str(). Can't write in 'r' mode: use 'w' or 'a'.

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 f.write('Hello') do?
Writes 'Hello' to the filePrints 'Hello'
Matches
❌ with open('f.txt','w') as f: f.write(42)
Error: write() needs a string
Applying
Meanings
Term
.write(string)
Meaning
Writes a string to the file.
Practice
with open('test.txt', 'w') as f:
Testing
Gaps
f.('Hello')
Check Answer
Spots
2with open('f.txt', 'w') as f:
3 f.write(score)

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

The 'Seal' badge — Smooth Formatter, 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 f.write('Hello') do?
A. Writes 'Hello' to the file ✓
B. Prints 'Hello'
write() sends text to the file — print() sends it to the screen
2. Does write() add a newline automatically?
A. Yes always
B. No you must add \n yourself ✓
Unlike print(), write() is literal — add '\n' to end each line
3. What does 'w' mode do to existing content?
A. Erases it completely ✓
B. Keeps it
'w' truncates the file to zero length before writing — use 'a' to preserve content
4. How do you write without erasing?
A. Use 'w' mode
B. Use 'a' mode (append) ✓
'a' (append) keeps existing content and adds new text after it
5. f.write('Hi') returns?
A. 2 (number of characters written) ✓
B. 'Hi'
write() returns the count of characters written — 'Hi' has 2 characters

Match the Pairs

with open('f.txt','w') as f: f.write(42) Error: write() needs a string
with open('f.txt','w') as f: pass f.write('hi') Error: File already closed
with open('f.txt','w') as f: f.write('Hello\n') Valid: Writes with newline
lines = ['a\n','b\n'] f.writelines(lines) Ok: Write list of strings
with open('f.txt','w') as f: n = f.write('Hi') print(n) Output: 2
with open('f.txt','w') as f: f.write('A') f.write('B') Output: (file has 'AB')

Key Term

.write(string)
Writes a string to the file.~~.writelines(list)

Fill in the Blanks

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

Spot the Error

Question 1
A score = 42
B with open('f.txt', 'w') as f:
C f.write(score)
D print('Saved!')
Line C — write() only accepts strings: convert number with str()
Question 2
A data = 'Hello World'
B with open('f.txt', 'r') as f:
C f.write(data)
D print('Written!')
Line B — Can't write in 'r' mode: use 'w' or 'a'
Question 3
A with open('f.txt', 'w') as f:
B f.write('Line 1')
C f.write('Line 2')
Line C — No newlines both lines will be on the same line: 'Line 1Line 2'
Question 4
A lines = ['a', 'b', 'c']
B with open('f.txt', 'w') as f:
C f.writelines(lines)
D print('Written!')
Line C — writelines doesn't add \n: strings run together: 'abc'
Question 5
A f = open('f.txt', 'w')
B f.write('Hello')
C # no close
Line A — File never closed data may not be saved. Use 'with'.
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?