How to Fix ValueError: i/o operation on closed file

Python ValueError is a built-in exception raised when a function or operation receives an argument with the right type but an inappropriate value.

In this problem-fixing article, we will discuss one ValueError exception often thrown by an interpreter while working with Python files.

ValueError: i/o operation on closed file

The ValueError: i/o operation on closed file error occurs when you try to operate on a closed file.

When a developer tries to read from or write to a file after he has closed it using the file.close() method, the Python interpreter raises the ValueError.

If you close the file by mistake or do not close it before the program exited, you will encounter the “i/o operation on closed file” error.

Python program that raises the error

Create a text file called app.txt inside your project folder and add the following content.

The Last Of Us

Now, write a Python program that opens the app.txt file, reads the content, closes it, again reads the opened file, and prints its content.

file = open('app.txt', 'r')
contents = file.read()
file.close()

contents = file.read()
print(contents)

Output

ValueError: I/O operation on closed file.

In this example, the open() function is used to open the file ‘app.txt’ in read mode.

We used the file.read() function to read the file’s contents and store them in a variable called contents.

To close a file, we used a file.close() method.

After closing the file, the program tries to read from the file again, but since the file has already been closed, the interpreter raises the “ValueError: i/o operation on closed file” error.

Now, we will address this error and see how to fix it.

How to fix ValueError: i/o operation on closed file

To fix ValueError: i/o operation on closed file error, you can use a with open() statement in Python.

The with open() statement allows you to open a file and automatically close it when you are done working with it. 

The with open() statement automatically closes the file even if an exception is thrown.

Python program that fixes the error

with open('app.txt', 'r') as file:
  contents = file.read()
  print(contents)

  contents = file.read()
  print(contents)

Output

The Last Of Us

In the above code, the open() function is used along with the “with statement” to open the file ‘app.txt’ in read mode.

We used the file.read() function to read the file’s contents, store them in the contents variable, and print them.

The file is not explicitly closed; it will be closed automatically when the block of the “with statement” is exited.

The program then attempts to read from the file again, but it works because the file is still open within the context of the “with statement”.

You can see from the program’s output that now, even after printing the contents, it does not throw the ValueError.

And that’s how you fix the error.

Summary

Use a with open() statement while working with files in Python because it automatically closes the file even if an exception is thrown.

Don’t try to operate on a closed file.

That’s pretty much it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.