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

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

  • Make sure the file exists: use os.listdir() to see the list of files in the current working directory
  • Make sure youre in the directory you think youre 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 = rC:Python32

    • If you dont 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 computers root directory, for example C:Pythonscripts if youre on Windows.
  • A relative path is a path that does not start with your computers root directory, and is instead relative to something called the working directory. You can view Pythons 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: Lets say file.txt is found in C:Folder.

To open it, you can do:

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

or

open(rC:Folderfile.txt) # absolute path

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

Its 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 doesnt have to be the location of your python script.

You have three options:

  • Use an absolute path to open the file:
    file = open(rC:pathtoyourfile.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(rC:pathtoyourfile)
    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:Usersnewtonfile.yaml
    # Incorrect! The n in Usersnewton is a line break character!
    

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

    path = rC:Usersnewtonfile.yaml
    # Correct!
    

    (See also: Windows path in Python)

  • Forgetting that Windows doesnt display file extensions:Since Windows doesnt display known file extensions, sometimes when you think your file is named file.yaml, its actually named file.yaml.yaml. Double-check your files extension.

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

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

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(rDrive:DirrecentlyUpdated.yaml)

Leave a Reply

Your email address will not be published. Required fields are marked *