How to handle FileNotFoundError when "try .. except IOError" does not catch it? How to handle FileNotFoundError when "try .. except IOError" does not catch it? python-3.x python-3.x

How to handle FileNotFoundError when "try .. except IOError" does not catch it?


FileNotFoundError is a subclass of OSError, catch that or the exception itself:

except OSError as e:

Operating System exceptions have been reworked in Python 3.3; IOError has been merged into OSError. See the PEP 3151: Reworking the OS and IO exception hierarchy section in the What's New documentation.

For more details the OS Exceptions section for more information, scroll down for a class hierarchy.

That said, your code should still just work as IOError is now an alias for OSError:

>>> IOError<class 'OSError'>

Make sure you are placing your exception handler in the correct location. Take a close look at the traceback for the exception to make sure you didn't miss where it is actually being raised. Last but not least, you did restart your Python script, right?