Closing a file with close()

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

Closing a file with close()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which line correctly closes a file object called notes?
A close(notes)
B notes.close()
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
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:
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

Match the Pairs

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

Left columnRight column
1f.close() Aclose 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.
2with open("scores.txt", "w") as f: Bf.close() f = open("scores.txt", "r")
3After writing to a file, close it so the data is saved and the file is released CEnds access to the open file, saves any written data and releases the file
4A file can be reopened after it has been closed, by calling open() again DThe file is never closed, so the written data may not be saved and the file stays locked. Add f.close() after writing.
5f = open("log.txt", "w") f.write("Saved") EOpens the file and closes it automatically when the indented block ends
6f.close f.write("Done") Ff.write("Ava,88") f.close()

Definitions

file.close()

Fill in the Blanks

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

1.
f = open("scores.txt", "w") f.write("Ava,88") f._____()
2.
____ open("log.txt", "w") as f: f.write("Saved")
3.
f = open("data.txt", "w") f.close() f = open("data.txt", "_")
4.
f = open("notes.txt", "a") f.write("Done") _.close()
5.
f = open("scores.txt", "w") f._____("Ava,88") f.close()

Spot the Error

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

Question 1
A f = open("a.txt", "w")
B f.close("Hi")
C f.close()
Question 2
A f = open("a.txt", "w")
B f.write("Hi")
C f.close()
D print(f.close)
Question 3
A with open("a.txt", "a") as f:
B f.write(Login)
C print(f.closed)
Question 4
A f = open("a.txt")
B f.write("Hi")
C f.close()
Question 5
A f = open("log.txt", "a")
B f.write("Login")
C f.close

Answer Key

Closing a file with close()
Section A: Quick Questions
Q1
A — It saves the written data and releases the file
Q2
B — notes.close()
Q3
A — Some of the written data may be lost
Q4
B — with open('data.txt', 'w') as f:
Q5
A — A ValueError is raised because the file is closed
Section B: Match the Pairs
Q1
f.close() → Ends access to the open file, saves any written data and releases the file
Q2
with open("scores.txt", "w") as f: → Opens the file and closes it automatically when the indented block ends
Q3
After writing to a file, close it so the data is saved and the file is released → f.write("Ava,88") f.close()
Q4
A file can be reopened after it has been closed, by calling open() again → f.close() f = open("scores.txt", "r")
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
f = open("scores.txt", "w") f.write("Ava,88") f.close()
Q2
with open("log.txt", "w") as f: f.write("Saved")
Q3
f = open("data.txt", "w") f.close() f = open("data.txt", "r")
Q4
f = open("notes.txt", "a") f.write("Done") f.close()
Q5
f = open("scores.txt", "w") f.write("Ava,88") f.close()
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
A — With no mode, open() defaults to read mode, so f.write() fails. Open with "w" to write, then close() saves the data.
Q5
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().