Borislav Hadzhiev
Last updated: Apr 24, 2022
Photo from Unsplash
The Python "ValueError: I/O operation on closed file" occurs when we try to
perform an operation on a closed file. To solve the error, make sure to indent
the code that tries to access the file correctly if using the with open()
statement.
Here is an example of how the error occurs.
import csv with open('employees.csv', 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['first_name', 'last_name'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) print(csvfile.closed) # 👉️ False # 👇️ forgot to indent code 👇️ # ⛔️ ValueError: I/O operation on closed file. print(csvfile.closed) # 👉️ True writer.writeheader() writer.writerow({'first_name': 'Alice', 'last_name': 'Smith'})
We forgot to indent the code that writes to the file, so the with open()
statement automatically closed the file and we performed an I/O operation on a
closed file.
To solve th error, make sure to indent your code correctly and move it into the
with
statement without mixing tabs and spaces.
import csv with open('employees.csv', 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['first_name', 'last_name'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) # ✅ code is now indented correctly writer.writeheader() writer.writerow({'first_name': 'Alice', 'last_name': 'Smith'}) writer.writerow({'first_name': 'Bob', 'last_name': 'Smith'})
The with open()
syntax takes care of automatically closing the file even if an
exception is thrown.
Here is another example.
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: # ✅ code is indented correctly my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n')
with open()
statement.Any code that tries to access the file needs to be indented correctly.
Alternatively, you can store the file object into a variable and manually close it.
file_name = 'example.txt' my_file = open(file_name, 'w', encoding='utf-8') my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') my_file.close() # 👈️ manually close file
Note that it's better to use the with open()
syntax as it automatically closes
the file after we are done.
You can use the closed
property on a file object to check if it's closed or
not.
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: # ✅ code is correctly indented my_file.write('first line' + '\n') my_file.write('second line' + '\n') my_file.write('third line' + '\n') print(my_file.closed) # 👉️ False print(my_file.closed) # 👉️ True
The last line of code is not indented, so the file is already closed at that point.