. Advertisement .
..3..
. Advertisement .
..4..
You might face the “Valueerror: I/O Operation On Closed File” when using a closed file. To deal with this Valueerror, make sure before the file is closed; the writing operation is put entirely. This post will help you fix the error conveniently.
When does the “Valueerror: I/O Operation On Closed File” happen?
The “Valueerror: I/O Operation On Closed File” occurs when you attempt to access a function/operation which takes an argument with the suitable type. However, the value is not appropriate.
Now, we will propose an example related to this Value error.
Example:
Suppose the csv file named example-file.csv includes the radii of the planets:
Mercury, 4878
Venus, 12104
Earth, 12756
Mars, 6787
Jupiter , 142796
Then, start writing a program from the csv file, and print it. You can import the csv library to the file. The code below will make you clear easily.
import csv
planets = open("example-planet.csv", "r")
read_file = csv.reader(planets)
planets.close()
for i in read_file:
print(f'Planet: {i[0]}, Diameter: {i[1]}Km')
Next, we make a “TextIOWrapper” object named particles and browse every line with particles by applying a “for” loop.
Traceback (most recent call last):
File "code.py", line 5, in <module>
for i in read_file:
ValueError: I/O operation on closed file.
After running the program, the “Valueerror: I/O operation on closed file” appears. From that, we draw to the main causes:
1. You close the file before iterating over it
In this case, this error appears since you close the file very early.
2. The loop over the file is not inside of the “with open()” statement
When you put code outside of the code block statement, the file will close and Valueerror will appear.
How to fix “Valueerror: I/O operation on closed file”?
Method 1: Make sure the file is closed after the “for” loop
To tackle the problem, we highly suggest that you ensure your file is closed after the ‘for’ loop.
import csv
planets = open("example-planet.csv", "r")
read_file = csv.reader(planets)
for i in read_file:
print(f'Planet: {i[0]}, Diameter: {i[1]}Km')
planets.close()
At this time, the code prints entirely the information of the planets.
Planet: Mercury, Diameter: 4878Km
Planet: Venus, Diameter: 12104Km
Planet: Earth, Diameter: 12756Km
Planet: Mars, Diameter: 6787Km
Planet: Jupiter , Diameter: 142796Km
As you can see, the error is solved completely.
Method 2: Place the close() statement outside the ‘for’ loop
As for this case, we highly recommend that the file contents can be iterated, and printed precisely.
import csv
# Open file in reading mode
planets = open("example-planet.csv", "r")
read_csv = csv.reader(planets)
# Iterate and print each row
for row in read_csv:
print("Rows: ", row)
planets.close()
Next, you run the program to get the outcome:
Rows: ['Mercury', ' 4878']
Rows: ['Venus', ' 12104']
Rows: ['Earth', ' 12756']
Rows: ['Mars', ' 6787']
Rows: ['Jupiter ', ' 142796']
As you can see from the result above, the code prints the result quickly. The error is removed entirely in this case.
Method 3: Indent the code appropriately and put it in the ‘with’ statement
To tackle this issue, you need to indent your code and put it in the ‘with’ statement.
import csv
# Open file in reading mode
with open("programming-language.csv", "r") as programming_language:
reader_csv = csv.reader(programming_language)
# Iterate and print the rows of csv
for row in reader_csv:
print("Rows: ", row)
Then, run the program and see the output below.
Rows: ['Java', ' 1996']
Rows: ['Ruby', ' 1995']
Rows: ['JavaScript', ' 1995']
Rows: ['Python', ' 1991']
Rows: ['TypeScript ', ' 2012']
Now, we make sure that you can solve this issue successfully.
Conclusion
Thanks for reading this useful post. In the other words, you can fix this “Valueerror: I/O Operation On Closed File” rapidly and simply. Do not forget to leave your response below this blog.
Read more:
→ How To Fix “ValueError: DataFrame constructor not properly called!” Error
Leave a comment