How to Fix the IO Error in Python

Here is how to fix IO errors (ValueError: I/O operation on closed file.) in Python. I analyzed the root of it and fixed the issue. Here my topic is what is the program? What is the IO error? How did I fix it?

IN THIS PAGE

  1. What is the program
  2. What is the IO error?
  3. How did I fix the error?
  4. Take Your SQL skill to Next level

The program’s logic is to get user input and write it into a CSV file. While writing I got an error.

What is the program

import csv
with open("mycsvfile1.csv","w") as myfile1:
    mywrite=csv.writer(myfile1) # CS1
    print(type(mywrite)) # CS2
while True:    
        myroll_no=input("Enter Student Roll No:") # CS4
        myname=input("Enter Student Name:") # CS5
        myage=input("Enter Student Age:") # CS6
        mycontactno=input("Enter Student contact number:") # CS7
        print(myfile1.closed)
        mywrite.writerow([myroll_no, myname, myage, mycontactno]) # CS8
        check = input("Do you want to enter one more student data: (Yes|No): ") # CS9
        if check.upper() == 'NO': # CS10
            break
print("Total Students data written to csv file successfully")  

What is the IO error?

The program was not successful and displayed an IO error.

<class '_csv.writer'>
Enter Student Roll No:
10
Enter Student Name:
Ravi
Enter Student Age:
23
Enter Student contact number:
112233445
True
Traceback (most recent call last):
  File "main.py", line 13, in <module>
    mywrite.writerow([myroll_no, myname, myage, mycontactno]) # CS8
ValueError: I/O operation on closed file.
** Process exited - Return Code: 1 **
Press Enter to exit terminal

How did I fix the error?

The code “while true break loop” is in the wrong place, which means it is outside the loop. So I have corrected the code. T

import csv
with open("mycsvfile1.csv","w") as myfile1:
    mywrite=csv.writer(myfile1) # CS1
    print(type(mywrite)) # CS2
    while True:    
        myroll_no=input("Enter Student Roll No:") # CS4
        myname=input("Enter Student Name:") # CS5
        myage=input("Enter Student Age:") # CS6
        mycontactno=input("Enter Student contact number:") # CS7
        print(myfile1.closed)
        mywrite.writerow([myroll_no, myname, myage, mycontactno]) # CS8
        check = input("Do you want to enter one more student data: (Yes|No): ") # CS9
        if check.upper() == 'NO': # CS10
            break
    print("Total Students data written to csv file successfully")  

The reason for the IO error is “while true break loop” is coded outside the loop. So the opened CSV file closes automatically by the python compiler. Due to this, while writing, it displays” ValueError: I/O operation on closed file.”.


Take your SQL skill to next level

Do you need SQL queries to practice? Here are the 32 complex SQL queries.

Related

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.