Warning: session_start(): open(/tmp/sess_2136d748d79c36df0d4655ed7d2b8f36, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Solve “ValueError: I/O operation on closed file" In Python - LearnShareIT

How To Solve “ValueError: I/O operation on closed file” In Python

ValueError: I/O operation on closed file in Python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *