46

I am always getting confused on whether a function would raise an IOError or OSError (or both?). What is the principle rule behind these exception types, what is the difference between them and when is which raised?

I've initially thought OSError is for things like permission denial, but opening a file without permissions will raise an IOError.

6
  • 11
    Changed in version 3.3: EnvironmentError, IOError, WindowsError, VMSError, socket.error, select.error and mmap.error have been merged into OSError. E.g. just throw OSError and forget about IOError.
    – Martijn Pieters
    Mar 30, 2015 at 13:44
  • @MartijnPieters Thanks, I've added the Python 2 tag. Just throwing OSError sounds good to me, yet I'm always having trouble to know when a function like shutil.copyfile() or os.access() would raise IOError or OSError (always have to look it up)
    – Niklas R
    Mar 30, 2015 at 13:44
  • 2
    Also see python.org/dev/peps/pep-3151 for the background on this, it'll help put the two exceptions into perspective.
    – Martijn Pieters
    Mar 30, 2015 at 13:44
  • 1
    If you are getting confused by it - well, that's one reason to switch to Python 3 then. Mar 30, 2015 at 13:46
  • 2
    @AnttiHaapala I do use Python 3 for standalone projects, but its a different story in an embedded Python environment. :)
    – Niklas R
    Mar 30, 2015 at 13:47

3 Answers 3

72

There is very little difference between the two types. In fact, even the core Python developers agreed that there is no real difference and removed IOError in Python 3 (it is now an alias for OSError). See PEP 3151 - Reworking the OS and IO exception hierarchy:

While some of these distinctions can be explained by implementation considerations, they are often not very logical at a higher level. The line separating OSError and IOError, for example, is often blurry. Consider the following:

>>> os.remove("fff")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: 'fff'
>>> open("fff")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'fff'

Yes, that's two different exception types with the exact same error message.

For your own code, stick to throwing OSError. For existing functions, check the documentation (it should detail what you need to catch), but you can safely catch both:

try:
    # ...
except (IOError, OSError):
    # handle error

Quoting the PEP again:

In fact, it is hard to think of any situation where OSError should be caught but not IOError, or the reverse.

2
  • or: except EnvironmentError: (which is then base class of WindowsError, mmap.error, shutil.Error, etc. too on PY2)
    – kxr
    Apr 28, 2017 at 22:49
  • As an aside, it's important to note that the problem isn't that there is no difference between O/S errors and I/O errors, but that Python was inconsistent. For an I/O error to be raised, Python should have stuck with "were it not for some error, specific to the device or request, the action would have succeeded." Jul 31, 2018 at 22:49
0

There's no difference between IOError and OSError cause they mostly appear on similar commands like opening a file or removing one.

0

Doesn't exacly answer the question but in case people don't know:

IOError stands for input/output error, and OSError stands for operating system error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.