Python OSError: Explained!

In this article, let us learn about OSError. Let us learn what it means and why it is named that way with the help of some examples.

To understand OSError, we must understand how the communication between Python and the operating system like Windows, Mac, Linux, etc, works.

One of the main duties of the Operating System (OS) is to manage the use of the actual hardware like our disk, RAM, CPU, internet connection, etc.

If our Python programs need to use the hardware of our computers they need to get permission from the OS for access to these resources.

All this communication between our Python programs and the operating system is done by the OS module (which in-essence is just a part of Python.)

The OS module provides several handy functions to help us interact with the underlying operating system. It contains a wide variety of file system-related calls that enable developers to perform tasks that involve OS-related I/O operations such as reading/writing to files, directories, getting access to the network, talking to connected devices such as USB devices, Bluetooth devices, etc.

An OSError is simply an exception that is raised when a problem has occurred while the OS Module in the Python interpreter is talking to the operating system to gain access to the hardware resources.

For instance, when you pass in invalid input to a file open call (like providing incorrect file names, or paths) a OSError will be raised.

Let’s exemplify what we have learned so far:

with open("non_existing_file.txt", "r") as my_file:

In the above example, we pass in an invalid input by passing in a file name that does not exist.

This results in an error:

Traceback (most recent call last):
  File "/home/", line 1, in <module>
    with open("non_existing_file.txt", "r") as my_file:
FileNotFoundError: [Errno 2] No such file or directory: 'non_existing_file.txt'

Keep in mind that FileNotFoundError is a subclass of OSError!

If you wish to learn the story behind this picture, I recommend reading our full article on the Hierarchy of Exceptions below.

Hierarchy of Exceptions in Python

Other subclasses of OSError include

  • FileExistsError: Raised when you are trying to create a new file with a name which has already been taken
  • InterruptedError: The action you tried to do has been interrupted by another process in the system
  • IsADirectoryError: You are trying to treat a directory like a file
  • NotADirectoryError: You are trying to treat a file like a directory
  • PermissionError: You do not have the admin rights needed to perform the action
  • BlockingIOError: Raised when an operation would block on an object (e.g. socket) set for non-blocking operation.
  • ChildProcessError: Raised when an operation on a child process failed.
  • TimeoutError: Raised when a system function timed out at the system level. 
  • ConnectionError: A base class for connection-related issues.

I suggest referring to the python documentation for more details, but if you notice the trend from the short explanations, all these errors are associated with access to the HW, which inturn is the duty of OS, hence OSErrors!

OSError is not one of the most common errors you will run into in your daily life as a Python developer. Instead, you need to focus on 7 other errors mentioned in the article below if you wish to gain mastery in Python!

7 Most Common In-Built Exceptions in Python!

If you are a visual learner here is a YouTube video that we made on that same topic!

And with that, I will end this article.

Congratulations on making it to the end of the article, not many have the perseverance to do so!

I hope you enjoyed reading this article and found it useful!

Feel free to share it with your friends and colleagues!

If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!

Related Articles

“Exception” Class in Python: A Walk-through!

Python: How to Create a Custom Exception

7 Most Common In-Built Exceptions in Python!

Python: Details contained in an Exception

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn