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
OSErrorandIOError, 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
OSErrorshould be caught but notIOError, or the reverse.
There's no difference between IOError and OSError cause they mostly appear on similar commands like opening a file or removing one.
For clarity, and because some error numbers can differ between platforms (see e.g. this comparison), I would recommend using the errno module in order to catch the specific type of OSError in a cross-platform way:
import errno
try:
# Code that might fail...
except OSError as e:
if e.errno == errno.ENOMEM:
# Handle ENOMEM case...
else:
raise
You can use the dictionary errno.errorcode to find the name of a specific error code in the errno module:
>>> errno.errorcode[12]
'ENOMEM'
Note that error number 12 specifically seems to be the same across most platforms, but many others differ.
You can use the errno attribute of the OSError. For an error:
>>> raise OSError(12, 'Some Error')
Traceback (most recent call last):
File "<ipython-input-5-8a046f16ebb6>", line 1, in <module>
raise OSError(12, 'Some Error')
OSError: [Errno 12] Some Error
Use the following:
try:
raise OSError(12, 'Some Error')
except OSError as e:
if e.errno == 12:
print('OSError no. 12 caught')
else:
raise
# Output:
# OSError: [Errno 12] Some Error
import os
try:
open('foo')
except IOError as err:
print(err)
print(err.args)
print(err.filename)
produces
[Errno 2] No such file or directory: 'foo'
(2, 'No such file or directory')
foo
So, to generate an OSError with a similar message use
raise OSError(2, 'No such file or directory', 'foo')
To get the error message for a given error code, you might want to use os.strerror:
>>> os.strerror(2)
'No such file or directory'
Also, you might want to use errno module to use the standard abbreviations for those errors:
>>> errno.ENOENT
2
>>> os.strerror(errno.ENOENT)
'No such file or directory'