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; FileNotFoundError was added, and 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?

Answer from Martijn Pieters on Stack Overflow
Discussions

Python's "open()" throws different errors for "file not found" - how to handle both exceptions? - Stack Overflow
However, when I wanted to run my ... is not defined. So, Python 3.2 on Windows thinks "FileNotFoundError" is a variable and the programs quits with an error. I figured out that Python 3.2 on Windows throws an "IOError" if the input filename is not valid.... More on stackoverflow.com
🌐 stackoverflow.com
Python - with open() except (FileNotFoundError)? - Stack Overflow
Unfortunately, this will catch any exception, not just File Not Found, and will disguise any program bugs that you may be unaware of. You can instead catch FileNotFoundError or IOError, depending on your version of Python. More on stackoverflow.com
🌐 stackoverflow.com
ERR: in python >= 3.5 use FileNotFoundError instead of OSError
FileNotFoundError, for specific errors such as this one. ... INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-31-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 pandas: 0.18.1 ... More on github.com
🌐 github.com
4
August 25, 2016
How to differentiate different IOErrors?
Sign up · Log in · Reset your password · Create account · Reset password · Create a new account More on forum.nim-lang.org
🌐 forum.nim-lang.org
November 1, 2020
🌐
Reddit
reddit.com › r/learnpython › what’s the difference between ioerror and oserror in python?
What’s the difference between IOError and OSError in python? : r/learnpython
November 11, 2020 - It also says IOError was merged with OSError. There is also a FileNotFoundError for when a file doesn't exist. Note that these are conventions, libraries can raise whatever they want. Your best bet is to read the documentation of the functions you're calling.
🌐
Sada Tech
tech.sadaalomma.com › python › difference-between-ioerror-and-file-not-found-error-in-python
Difference Between Ioerror and File Not Found Error in Python - SADA Tech
February 25, 2024 - Scope: IOError is broader, while FileNotFoundError is specific to missing files or directories. Python Version: FileNotFoundError is available from Python 3.3 onwards, whereas IOError has been present since earlier versions.
🌐
Initial Commit
initialcommit.com › blog › python-ioerror
IOError in Python | How to Solve with Examples - Initial Commit
Though the error raised is a FileNotFoundError, which is a subclass of the familiar IOError, it still serves to show that any failed alteration during an input or output function will result in an error.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 14086
ERR: in python >= 3.5 use FileNotFoundError instead of OSError · Issue #14086 · pandas-dev/pandas
August 25, 2016 - Simple to reproduce: import pandas as pd try: pd.read_csv('this_file_does_not_exist.csv') except FileNotFoundError: print('This should execute.') Instead, I get an OSError, which tells me in text that OSError: File b'this_file_does_not_e...
Author: pandas-dev
Find elsewhere
🌐
UW PCE
uwpce-pythoncert.github.io › ProgrammingInPython › modules › Exceptions.html
Exception Handling — Programming in Python 7.0 documentation
In this case, it’s opening and processing a file. But if the file isn’t there, then a FileNotFoundError is “raised”. When an Exception is raised, no further code is run – so the process() function will not be called. Once an exception is raised, Python looks for an except line.
🌐
Apache JIRA
issues.apache.org › jira › browse › ARROW-7282
[ARROW-7282] [Python] IO functions should raise FileNotFoundError when appropriate - ASF JIRA
January 11, 2023 - In my opinion, this particular error should also subclass from Python FileNotFoundError. It currently only inherits from IOError (which is a superclass of FileNotFoundError).
🌐
Medium
learncsdesigns.medium.com › understanding-errors-exceptions-file-i-o-in-python-4ef2cac987e2
Understanding Errors, Exceptions & File I/O In Python | by Neeraj Kushwaha | Medium
January 29, 2023 - In this example, the try block attempts to open and read the contents of the file “file.txt”. If the file is not found, the program will execute the code in the except FileNotFoundError block and print an error message. If the file is not open, the program will execute the code in the except ValueError block and print an error message.
🌐
LabEx
labex.io › tutorials › python-how-to-handle-file-not-found-error-in-python-397683
How to handle file not found error in Python | LabEx
Always wrap your file operations in try-except blocks to handle FileNotFoundError and other potential exceptions like IOError.
🌐
Nim Forum
forum.nim-lang.org › t › 7133
How to differentiate different IOErrors?
November 1, 2020 - Sign up · Log in · Reset your password · Create account · Reset password · Create a new account
🌐
Reddit
reddit.com › r/learnpython › nameerror: name 'filenotfounderror' is not defined
r/learnpython on Reddit: NameError: name 'FileNotFoundError' is not defined
January 9, 2018 -

I'm learning about try-except blocks but I'm encountering this Name Error whenever I try to deal with a potential File Not Found Error. Here is my code:

try:
    with open("mytext.txt", "r") as f_obj:
        x = f_obj.read()
        print (x)
except FileNotFoundError:
    print "You don't have this file, bro"

I run the script, and I get "NameError: name 'FileNotFoundError' is not defined"..Any ideas?

🌐
Georgevreilly
georgevreilly.com › blog › 2016 › 03 › 24 › RaisingIOErrorForFileNotFound.html
Raising IOError for 'file not found' - George V. Reilly
March 24, 2016 - You may object that opening a file only after checking for its existence is fragile because there’s a small window between checking and opening where the file could be removed by another process. You’d be right. In such a case, open(filename) will raise IOError(ENOENT, "No such file or directory", filename).
🌐
Python
docs.python.org › 3 › builtins › exceptions.html
Built-in Exceptions — Python 3.14.7 documentation
Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.
🌐
AskPython
askpython.com › python › examples › python-filenotfounderror
[SOLVED] Python filenotfounderror - A Quick Guide - AskPython
February 16, 2023 - If you have worked with Python before, you would have faced this situation too, where you write all your code, maintain proper indents in it, put comments line, double-check for mistypes, and after making sure that everything is right and in its place, you run the code and end up getting ‘filenotfounderror’ in the compiler line. Frustrating, isn’t it? Don’t worry, we will make sure to cover all possible ways to resolve this problem so that you do not encounter it ever again. Also read: Handling IOErrors in Python – A Complete Guide
🌐
CareerKarma
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory Solution Wiki | Career Karma
Any message with the contents FileNotFoundError indicates that Python cannot find the file you are referencing. Python raises this error because your program cannot continue running without being able to access the file to which your program refers.
🌐
TutorialsPoint
tutorialspoint.com › How-to-catch-IOError-Exception-in-Python
How to catch IOError Exception in Python?
In Python, an IOError (or OSError in latest versions) occurs when an input/output operation fails. For example, when we are trying to read a file that doesn’t exist, writing to a file that is read-only, or accessing a corrupted device. You can catch