
The “ValueError: I/O operation on closed file” error in Python is a common error related to the file i/o operation. You can read the explanations below to have more knowledge about this error including cause and solutions.
How does the “ValueError: I/O operation on closed file” error in Python happen?
This error happens due to the many following reasons:
Firstly, it might be because you are working on a closed file. If a file is open, you might not get the error. But if the file is closed and you are trying to write or read that file, you will get this error. For example:
with open("file0", "w") as file0: if file0.closed: print("File closed") else: print("File not closed") print("Out of with statement:") if file0.closed: print("File closed") else: print("File not closed") file0.write("You cannot write to file after closed")
Secondly, after working with long lines of code, you make the mistake of reading two files but assigning the same name to the two file handles, such as in the below example. Attention to the nested “with open” statement.
with open("file0", "w") as f: f.write("Write to file0") with open("file1", "w") as f: f.write("Write to file1") f.write("Cannot write to file0 because it is closed")
How to solve the error?
Solution 1: Checking indent of coding
This error often occurs when the coder type a wrong code and cannot indent code correctly, resulting in the system automatically closing the file. Hence, they cannot do any I/O operation on that file. To solve it, you should recheck your indent of the write or read commands to ensure it is inside the with
statement.
with open("file0", "w") as file0: file0.write("Writing in the with statement")
Another way to avoid this error is to have an if
statement to check if the file is closed before we try any I/O operations. For instance:
with open("file0", "w") as file0: if file0.closed is False: file0.write("Writing in the with statement") print("Out of with statement:") if file0.closed is False: file0.write("You cannot write to file after closed")
Solution 2: Handling two files with different names
Remember that in Python, you can redeclare a variable with an already name. This would lead to some of your file handles being possible to have the same names. However, we do not recommend this as it is difficult to manage and use. Instead, you should have a different name for each file handle, such as:
with open("file0", "w") as f: f.write("Write to file0") with open("file1", "w") as f1: f1.write("Write to file1") f.write("The file0 is still open, so you can write to it")
As we have recommended in the first solution, you should have an if
statement to make sure the file is opening before approaching it:
with open("file0", "w") as f: f.write("Write to file0") with open("file1", "w") as f1: if f1.closed is False: f1.write("Write to file1") if f.closed is False: f.write("Cannot write to file0 because it is closed")
Summary
We have learned how to deal with the “ValueError: I/O operation on closed file” error in Python. By checking the indent of the statements and changing names for different file handles as guided in our tutorial, you can easily solve it.
Maybe you are interested:
- ValueError: invalid literal for int() with base 10 in Python
- ValueError: object too deep for desired array

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R