← QCE Digital Solutions Unit 3

Closing a file with close()

Ends access to an open file so any written data is saved and the file is released. Forgetting it can lose data. A with statement closes the file for you automatically.

Understanding Closing a file with close() in QCE Digital Solutions Unit 3

What it does: The close() call comes after the write so the written data is saved to the file and the file is released. Once closed, the old file object cannot be used, so open() is called again to get a new file object. The with statement closes the file for you, even if an error occurs inside the block. An extra close() is unnecessary.

Common mistakes: close() takes no arguments and does not write anything. Line 2 should be f.write("Hi"), and close() comes after it to save the data. f.close is the method itself, so this prints a method object. To check whether the file is closed, use the attribute f.closed, which gives True or False.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 251–337) and word search (Modules 194–337).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does file.close() do after you have finished writing to a file?
It saves the written data and releases the fileIt deletes the file from the disk
Matches
f = open("log.txt", "w") f.write("Saved")
↓
The file is never closed, so the written data may not be saved and the file stays locked. Add f.close() after writing.
Applying
Meanings
Term
file.close()
Meaning
Ends access to an open file so any written data is saved and the file is released. Forgetting it can lose data. A with statement closes the file for you automatically.
Practice
f = open('notes.txt', 'w')
Testing
Gaps
f.()
Check Answer
Spots
1f = open("a.txt", "w")
2f.close("Hi")

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

The 'Dolphin' badge — Reef Streak, 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 file.close() do after you have finished writing to a file?
A. It saves the written data and releases the file ✓
B. It deletes the file from the disk
close() ends access to the open file. Any buffered data is saved and the file is released so other programs can use it.
2. Which line correctly closes a file object called notes?
A. close(notes)
B. notes.close() ✓
close() is a method called on the file object, so you write notes.close() with the brackets. close(notes) is not valid Python.
3. A student writes to scores.txt but never closes the file. What could happen?
A. Some of the written data may be lost ✓
B. Python automatically deletes the file
Written data can sit in a buffer. If the file is never closed, it may not be saved to the disk, so data can be lost.
4. Which code closes the file automatically when the block ends?
A. f = open('data.txt', 'w')
B. with open('data.txt', 'w') as f: ✓
A with statement closes the file for you when its indented block finishes. A plain open() needs a separate f.close() call.
5. After f.close(), what happens if you run f.write('Hi')?
A. A ValueError is raised because the file is closed ✓
B. The text is added to the end of the file
You cannot use a closed file. Python raises ValueError: I/O operation on closed file.

Match the Pairs

f.close() → Ends access to the open file, saves any written data and releases the file
with open("scores.txt", "w") as f: → Opens the file and closes it automatically when the indented block ends
After writing to a file, close it so the data is saved and the file is released → f.write("Ava,88") f.close()
A file can be reopened after it has been closed, by calling open() again → f.close() f = open("scores.txt", "r")
f = open("log.txt", "w") f.write("Saved") → The file is never closed, so the written data may not be saved and the file stays locked. Add f.close() after writing.
f.close f.write("Done") → close is missing its brackets so it never runs, and write() is called after the file should be closed. Use f.close() after the last write.

Key Term

file.close()
Ends access to an open file so any written data is saved and the file is released. Forgetting it can lose data. A with statement closes the file for you automatically.

Fill in the Blanks

1. f = open("scores.txt", "w") f.write("Ava,88") f.close()
Hint: Type the method name that ends access to the file and saves what was written.
2. with open("log.txt", "w") as f: f.write("Saved")
Hint: Type the keyword that closes the file for you automatically when its block ends.
3. f = open("data.txt", "w") f.close() f = open("data.txt", "r")
Hint: Type the single-letter mode that reopens the closed file so it can be read.
4. f = open("notes.txt", "a") f.write("Done") f.close()
Hint: Type the variable name of the open file that must be closed.
5. f = open("scores.txt", "w") f.write("Ava,88") f.close()
Hint: Type the method that puts text into the file before it is closed and saved.

Spot the Error

Question 1
A f = open("a.txt", "w")
B f.close("Hi")
C f.close()
Line B — close() takes no arguments and does not write anything. Line 2 should be f.write("Hi"), and close() comes after it to save the data.
Question 2
A f = open("a.txt", "w")
B f.write("Hi")
C f.close()
D print(f.close)
Line D — f.close is the method itself, so this prints a method object. To check whether the file is closed, use the attribute f.closed, which gives True or False.
Question 3
A with open("a.txt", "a") as f:
B f.write(Login)
C print(f.closed)
Line B — Login has no quotes, so Python treats it as a variable name and raises a NameError. The text must be a string: f.write("Login"). The with statement then closes the file when the block ends.
Question 4
A f = open("a.txt")
B f.write("Hi")
C f.close()
Line A — With no mode, open() defaults to read mode, so f.write() fails. Open with "w" to write, then close() saves the data.
Question 5
A f = open("log.txt", "a")
B f.write("Login")
C f.close
Line C — Without brackets, f.close just names the method and never calls it. The file stays open and data may be lost. It should be f.close().

Student with a class code? Join your class →