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
🌐
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. ... I finished Helsinki's Advanced Programming course and would like to know what some popular courses are that could come after this one. Perhaps about DSA or other. ... Python ...
People also ask

How do I fix the Python FileNotFoundError Errno 2 No such file or directory?
To fix FileNotFoundError: [Errno 2] No such file or directory error, check that you are referring to the right file or folder in your program. Here is an example scenario featuring a FileNotFoundError message. An Example Scenario We’re writing a program that lists all the files in a folder. The folder we are referencing contains a list of markdown documentation for our project. To start, let’s import the os library, which has a method that lets us see all the files in a folder: import os Next, we are going to use the os.listdir() method to get a list of the files in our folder: for f in os.
🌐
careerkarma.com
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory ...
What does the FileNotFoundError: [Errno 2] No such file or directory error mean?
The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.
🌐
careerkarma.com
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory ...
Why am I seeing a FileNotFoundError: [Errno 2] No such file or directory error?
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. This error is usually raised when you use the os library. You will see an IOError if you try to read or write to a file that does not exist using an open() statement.
🌐
careerkarma.com
careerkarma.com › feed › python filenotfounderror: [errno 2] no such file or directory solution
Python FileNotFoundError: [Errno 2] No such file or directory ...
🌐
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.
🌐
Reddit
reddit.com › r/learnpython › python giving "file not found" error with both file in same directory and using absolute path
r/learnpython on Reddit: Python giving "file not found" error with both file in same directory and using absolute path
August 6, 2017 -

[Solved] I am an idiot. I had a folder called KanjiDrag, and within that I had the actual source folder kanji_drag which is where the json file and the main module were. The path I was using was accessing the KanjiDrag folder, but not the kanji_drag folder, and I didn't catch the different names. This is my dumbest file IO mistake yet. Thanks for all the replies, many of which I'll still refer to later when I refine this part of my program. Upvotes for everyone!

Hi, all. I'm running into a "no such file or directory" issue in python that I can't figure out.

I'm using windows 7, and I have the file in the same directory as the program I'm running. I got this error, so then I tried using the absolute path (the file is still in the same working directory anyway) and still got the same error. I even checked with os.getcwd and os.path.abspath and copy-pasted the path exactly.

I'm not sure what's going on here. I closed every program that could possibly be running the file. Would I be getting this error if the file is still open in some elusive background program?

This is the relevant bit of code:

print(os.getcwd())
print(os.path.abspath('RainyGenki1&2.json'))

deckName = "C:\Users\myName\My Documents\LiClipse Workspace\KanjiDrag\RainyGenki1&2.json"
deck = open(deckName, 'r')   #opens card deck
Find elsewhere
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 67325353 › opening-json-file-error-filenotfounderror-errno-2-no-such-file-or-directory › 68744978
python 3.x - opening json file error : FileNotFoundError: [Errno 2] No such file or directory - Stack Overflow
1 Django: IOError [Errno 2] No such file or directory while Reading JSON file path using python · 1 Exception has occurred: FileNotFoundError [Errno 2] No such file or directory: 'data.json' 0 FileNotFoundError but file exists · 0 Python JSON Loading file errors ·
🌐
Stack Overflow
stackoverflow.com › questions › 59322801 › ioerror-errno-2-no-such-file-or-directory-python-i-am-having-this-error-ev
json - IOError: [Errno 2] No such file or directory: (Python: I am having this error even though the file does exist) - Stack Overflow
73 open file in "w" mode: IOError: [Errno 2] No such file or directory · 894 open() in Python does not create a file if it doesn't exist · 115 open() gives FileNotFoundError/IOError: Errno 2 No such file or directory · 181 "OSError: [Errno 2] No such file or directory" while using python subprocess with command and arguments ·
🌐
Reddit
reddit.com › r/learnprogramming › [python] filenotfounderror: [errno 2] no such file or directory: 'foo.json'
r/learnprogramming on Reddit: [Python] FileNotFoundError: [Errno 2] No such file or directory: 'foo.json'
February 2, 2022 -

I'm getting this error, despite the file existing. My code is

file = open('./foo.json')
data = json.load(file)
file.close()
return data

The file does exist, in the same directory. I have also tried

file = open(os.path.join(sys.path[0], "foo.json"), "r")

But I get the same error. I think this might be happening because my code is running in a docker container. Any tips?

EDIT: I was able to solve the issue. Use

print(os.listdir())

Then fix the path from there

🌐
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).
🌐
OneUptime
oneuptime.com › home › blog › how to fix 'filenotfounderror' in python
How to Fix 'FileNotFoundError' in Python
January 25, 2026 - Python Debugging Error Handling Common Errors File Operations · The FileNotFoundError occurs when Python tries to access a file or directory that does not exist.
Top answer
1 of 13
173

Let me clarify how Python finds files:

  • An absolute path is a path that starts with your computer's root directory, for example C:\Python\scripts if you're on Windows.
  • A relative path is a path that does not start with your computer's root directory, and is instead relative to something called the working directory.

If you try to do open('recentlyUpdated.yaml'), Python will see that you are passing it a relative path, so it will search for the file inside the current working directory.

To diagnose the problem:

  • Ensure the file exists (and has the right file extension): use os.listdir() to see the list of files in the current working directory.
  • Ensure you're in the expected directory using os.getcwd().
    (If you launch your code from an IDE, you may be in a different directory.)

You can then either:

  • Call os.chdir(dir) where dir is the directory containing the file. This will change the current working directory. Then, open the file using just its name, e.g. open("file.txt").
  • Specify an absolute path to the file in your open call.

By the way:

  • Use a raw string (r"") if your path uses backslashes, like so: dir = r'C:\Python32'
    • If you don't use raw string, you have to escape every backslash: 'C:\\User\\Bob\\...'
    • Forward-slashes also work on Windows 'C:/Python32' and do not need to be escaped.

Example: Let's say file.txt is found in C:\Folder.

To open it, you can do:

os.chdir(r'C:\Folder')
open('file.txt')  # relative path, looks inside the current working directory

or

open(r'C:\Folder\file.txt')  # absolute path
2 of 13
41

Most likely, the problem is that you're using a relative file path to open the file, but the current working directory isn't set to what you think it is.

It's a common misconception that relative paths are relative to the location of the python script, but this is untrue. Relative file paths are always relative to the current working directory, and the current working directory doesn't have to be the location of your python script.

You have three options:

  • Use an absolute path to open the file:

    file = open(r'C:\path\to\your\file.yaml')
    
  • Generate the path to the file relative to your python script:

    from pathlib import Path
    
    script_location = Path(__file__).absolute().parent
    file_location = script_location / 'file.yaml'
    file = file_location.open()
    

    (See also: How do I get the path and name of the file that is currently executing?)

  • Change the current working directory before opening the file:

    import os
    
    os.chdir(r'C:\path\to\your\file')
    file = open('file.yaml')
    

Other common mistakes that could cause a "file not found" error include:

  • Accidentally using escape sequences in a file path:

    path = 'C:\Users\newton\file.yaml'
    # Incorrect! The '\n' in 'Users\newton' is a line break character!
    

    To avoid making this mistake, remember to use raw string literals for file paths:

    path = r'C:\Users\newton\file.yaml'
    # Correct!
    

    (See also: Windows path in Python)

  • Forgetting that Windows doesn't display file extensions:

    Since Windows doesn't display known file extensions, sometimes when you think your file is named file.yaml, it's actually named file.yaml.yaml. Double-check your file's extension.

🌐
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?