FileNotFoundError: [Errno 2] No such file or directory

Are you getting the error “filenotfounderror: [errno 2] no such file or directory”? Yes, you have come to the right place. Today I will show you how to solve this error.

This error occurs when we try to open a file that does not exist in the specified location.

[Fixed] Python requests max retries exceeded with URL

[Fixed]: FileNotFoundError: [Errno 2] No such file or directory

You can fix this error by moving the file to a directory where the python file is located. The second solution is to use the absolute path of the file you are trying to read in the Python program.

Here is an example of how this error occurs:

# FileNotFoundError: [Errno 2] No such file or directory: 'newfile.txt'
with open('newfile.txt', 'r') as f:
    lines = f.readlines()
    print(lines)

In the above code, we are trying to read a file named “newfile.txt” but the file is not located in the same directory as our Python file. Because of this, we are getting this error “filenotfounderror: [errno 2] no such file or directory:”.

How to solve this error?

The first way to solve this error is to move the file to the directory where the python file is located. Let’s say our python file is located in a directory named “my_folder” then our “newfile.txt” should also be in this folder “my_folder”.

Alternative solution for filenotfounderror errno 2 no such file or directory

Another solution to this error is to use the absolute path of the file in the python program.

Your file’s absolute path may look like this one:

r'C:\Users\Ehtisham\Desktop\newfile.txt'

If you use the absolute path of the file then you don’t need to move your file to the same folder as your python file directory.

You can use the absolute path like this:

absolute_path = r'C:\Users\Ehtisham\Desktop\newfile.txt'

with open(absolute_path , 'r') as f:
    lines = f.readlines()

    print(lines)

I your “newfile.txt” is not in the same folder the above code will solve your issues. You don’t need to move your file to the same folder.

[Solved]: Using the OS Module – FileNotFoundError: [Errno 2] No such file or directory

You can solve this error in another way. You can change the current directory in your Python using the OS module, as shown in the code below:

import os
file_dir = r'C:\Users\Ehtisham\Desktop\newfile.txt'
os.chdir(file_dir)

file_name = 'newfile.txt'

with open(file_name, 'r') as f:
    lines = f.readlines()

    print(lines)

Python create folder if doesn’t exist:

You can create folder in pyhton by checking if it is not exists. python create new folder if not exist code:

import os
if not os.path.exists("path/to/folder"):
    os.makedirs("path/to/folder")

Conclusion on FileNotFoundError: [Errno 2] No such file or directory

Programmers, we discussed how we can solve this error: “filenotfounderror: [errno 2] no such file or directory.” If you have any questions related to the tutorial or are still facing the error, please let us know in the comments section.

This error “filenotfounderror errno 2 no such file or directory” can be solved by moving the file to the same directory as your python file. or using the absolute path of the file.

Leave a Reply