open() gives FileNotFoundError/IOError: Errno 2 No such file or directory

PythonFileFile IoFilenotfoundexceptionFile Not-Found

Python Problem Overview


For some reason my code is having trouble opening a simple file:

This is the code:

file1 = open('recentlyUpdated.yaml')

And the error is:

IOError: [Errno 2] No such file or directory: 'recentlyUpdated.yaml'
  • Naturally I checked that this is the correct name of the file.
  • I have tried moving around the file, giving open() the full path to the file and none of it seems to work.

Python Solutions


Solution 1 - Python

  • Make sure the file exists: use os.listdir() to see the list of files in the current working directory
  • Make sure you're in the directory you think you're in with os.getcwd() (if you launch your code from an IDE, you may well be in a different directory)
  • You can then either:
    • Call os.chdir(dir), dir being the folder where the file is located, then open the file with just its name like you were doing.
    • Specify an absolute path to the file in your open call.
  • Remember to use a raw string 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.

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. You can view Python's current working directory by calling os.getcwd().

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

Calling os.chdir() will change the current working directory.

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

Solution 2 - Python

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:


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: https://stackoverflow.com/questions/2953834/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.

Solution 3 - Python

The file may be existing but may have a different path. Try writing the absolute path for the file.

Try os.listdir() function to check that atleast python sees the file.

Try it like this:

file1 = open(r'Drive:\Dir\recentlyUpdated.yaml')

Solution 4 - Python

Possibly, you closed the 'file1'.
Just use 'w' flag, that create new file:

file1 = open('recentlyUpdated.yaml', 'w')

> mode is an optional string that specifies the mode in which the file > is opened. It defaults to 'r' which means open for reading in text > mode. Other common values are 'w' for writing (truncating the file if > it already exists)...

(see also https://docs.python.org/3/library/functions.html?highlight=open#open)

Solution 5 - Python

If is VSCode see the workspace. If you are in other workspace this error can rise

Solution 6 - Python

Check the path that has been mentioned, if it's absolute or relative.

If its something like-->/folder/subfolder/file -->Computer will search for folder in root directory.

If its something like--> ./folder/subfolder/file --> Computer will search for folder in current working directory.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSantiagoView Question on Stackoverflow
Solution 1 - PythonLanaruView Answer on Stackoverflow
Solution 2 - PythonAran-FeyView Answer on Stackoverflow
Solution 3 - PythonheretolearnView Answer on Stackoverflow
Solution 4 - PythonhesedView Answer on Stackoverflow
Solution 5 - PythonzzfimaView Answer on Stackoverflow
Solution 6 - PythonVortexView Answer on Stackoverflow