Filenotfounderror Errno 2 no such file or directory : Python Error (Solution)

Filenotfounderror Errno 2 no such file or directory Solution

Filenotfounderror Errno 2 no such file or directory is a python error always comes when you are not defining proper path for the file or file does not exist in the directory. In this entire tutorial, you will know how to solve Filenotfounderror Errno 2 no such file or directory in an easy way in different scenarios.

 

Before going to the various scenarios let’s create a sample CSV file using the panda’s library. The file will contain the name and age of the person. Execute the below line of code to create a person.csv file. It is only for demonstration purposes. You can move to cases if you are already getting the problem.

import pandas as pd
data = {"name":["Sahil","Rob","Maya"],"age":[23,67,45]}
df = pd.DataFrame(data)
df.to_csv("person.csv")

It will save the person.csv file to the current working directory of the project.

Filenotfounderror Errno 2 no such file or directory Cases

Case 1: File Name is Incorrect

If you are reading the CSV file with the incorrect name then you will get this Filenotfounderror Errno 2 with no such file or directory error. For example, instead of reading the person.csv filename, I am reading persons.csv. Then you will get this filenotfounderror.

import pandas as pd
df = pd.read_csv("persons.csv")
print(df)
FileNotfounderror on reading the wrong filename
FileNotfounderror on reading the wrong filename

Solution 

Check the name of the file and write the correct filename with its type.

Case 2: Using the OS Library

Filenotfounderror Errno 2 no such file or directory error also comes when you are using the OS Python library and defining the wrong path. For example, I am passing the wrong path for filename “persons.csv”. It will give me an error.

Solution 

Check the path of the working directory and then define the path.

Case 3: Passing the wrong file name or path for the open() method

The third case is when this error comes when you are reading the file using the open() method and passing the wrong filename.

import csv
with open('persons.csv','r') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
FileNotfounderrro on reading the wrong filename using the open() method
FileNotfounderrro on reading the wrong filename using the open() method

Solution :

The solution to this problem, in this case, is very simple. Check the filename of the file you want to open and then pass the exact path for the filename for that file.  To know the current working directory you have to use the os.getcwd(). The error will be solved.

['', 'name', 'age']
['0', 'Sahil', '23']
['1', 'Rob', '67']
['2', 'Maya', '45']

Case 4: Wrong Directory

In most of the cases, Filenotfounderror no such file or directory error comes when you are defining the wrong path for the filename.

import pandas as pd
df = pd.read_csv("/foo/persons.csv")
print(df)
FileNotfounderrro python error due to wrong file path
FileNotfounderrro python error due to wrong file path

Solution

The solution of this case is that if have forgotten the path for the filename then you have to use the OS library. There is a method for finding the path and it is os.getcwd() and then use it with the filename. Execute the following lines of code.

import pandas as pd
import os
cwd = os.getcwd()
df = pd.read_csv(f'{cwd}/person.csv')
print(df)

Now you will get the output.

Solution for FileNotFoundError using the OS Package
Solution for FileNotFoundError using the OS Package

Conclusion

This type of error is mostly annoying to every programmer. They often get this error. These are the cases for various scenarios where its solution is very simple. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner