Handling IOErrors in Python – A Complete Guide


In this article, we’ll learn about handling IOErrors in Python Let us say that we are performing a mathematical operation on a particular example. This can be more tragic when it is lengthy. The main problem occurs when we are stuck somewhere. With a lot of effort, we solve it. But, the answer is not satisfactory or it is wrong. There are two possibilities for this:

  1. Either the problem we are trying to solve is incorrectly built from the start us.
  2. Or we are giving wrong input in the whole process or steps.

The whole thing in one simple word is an error. They can be of various types in various conditions. It depends on the problem itself. In the same way, there are errors in programming. They are the different forms of output and occur in special cases. 

What is an IOError in Python?

IOError means Input/Output error. It occurs when a file, file path, or OS operation we’re referencing does not exist. For example, if you are running a runtime operation on an existing file, and the file goes missing from the location, Python will throw an IOError.

Now, before we learn how to handle IOErrors in Python, let’s understand the different types of errors.

Types Of Errors in Python

Compilers segment errors into different categories for better identification and solutions. Below are some of the most common error types that you’ll encounter during your programming.

  1. ZeroDivisionError: Occurs when we try to divide a number by zero.
  2. AssertionError: When the debugging or assert statement of a Python script fails this comes out.
  3. AttributeError: When the given attribute is wrong or does not exist in a module or script.
  4. FloatingPointError: Error in the floating point implementation process.
  5. ImportError/ModuleNotFoundError: If we try to import a module and it does not exist, then this raises.
  6. IOError : Raised when a file we are trying to access does not exist in the system.

You can browse more on the different exceptions from the official Python documentation through this link.

Detecting and Handling IOErrors in Python

Generally, in newer Python versions this exception has a new name.

Handling IOErrors in Python During File Operations

Let’s create a function to reference a file and then we’ll handle the IOError.


file = open('sample.txt', 'w')
print('The file name is: ', file.name)
print('Openeing mode: ', file.mode)
Opening A File Detecting and Handling IOErrors in Python
Opening A File
print('File is closed: ', file.closed)
Closing A File 2 Detecting and Handling IOErrors in Python
Closing a file

Now, we will delete the file and then try to open it and this will raise the required error.


FileNotFoundError 1 Detecting and Handling IOErrors in Python
FileNotFoundError occurred

FileNotFoundError is a subclass of IOError. We can also detect it using the Exception Handling methods in Python.

Let’s use the try and catch block to handle our filenotfounderror and provide us with a better, more understandable output.

    file = open('sample.txt', 'w')
    print('File found!!!')
except IOError:
    print('File not found!!!')

# Output: File not found!!!
Error Detection
Error Detection


  1. In the try block, we try to open the file in read mode.
  2. Then we add a message that if the file exists then print ‘file found’.
  3. If the file does not exist the except statement takes care of that.
  4. When an error occurs, this block will catch the error and print ‘File not found‘ instead of the complex error message we saw before.


So, the topic of handling IOError ends. This error specifically comes under the file handling criteria of Python programming. It is an easy topic to study and we can just get rid of errors using the ‘try-except blocks. Revise the simple code once again for getting a more clear idea.

I hope you enjoyed this really short and simple guide on working with IOErrors in Python.