close

[Solved] Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory Error Occurs?

Today I get the following error Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory in python.

How To Solve Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory Error ?

  1. How To Solve Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory Error ?

    To Solve Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory Error The file may be existing but may have a different path. Try writing the absolute path for the file.

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

    To Solve Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory Error The file may be existing but may have a different path. Try writing the absolute path for the file.

Solution 1


  • 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.
  • 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') #full path

Solution 2

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')

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read