close

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

Hello Guys, How are you all? Hope You all Are Fine. I am trying to read the file but for some reason python cannot locate it and gives me the error FileNotFoundError: [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, Lets start This Article to Solve This Error.

How FileNotFoundError: [Errno 2] No such file or directory Error Occurs ?

I am trying to read the file but for some reason python cannot locate it and gives me the error.

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

Here is what I am trying to read

import csv

with open('locations.csv','r') as f:

How To Solve FileNotFoundError: [Errno 2] No such file or directory Error?

Question: How To Solve FileNotFoundError: [Errno 2] No such file or directory Error?
Answer: To Solve FileNotFoundError: [Errno 2] No such file or directory Error Maybe You are using a relative path, which means that the program looks for the file in the working directory. The error is telling you that there is no file of that name in the working directory. Try using the exact, or absolute, path.

Solution 1

I think some problem occurs to assign path. Just make sure your file is been there where you specify file name. To give you an idea of what that means, add this to your code:

import os

cwd = os.getcwd()  # your current working directory (cwd)
files = os.listdir(cwd)  # This will Get all the files in that directory
print("Files in %r: %s" % (cwd, files))

Or Just tell the open() function where your file is located is by using an absolute path, For example:

f = open("/Users/admin/locations.csv")

Solution 2

Maybe You are using a relative path, which means that the program looks for the file in the working directory. The error is telling you that there is no file of that name in the working directory. Try using the exact, or absolute, path.

Solution 3

Use the exact path.

import csv


with open('C:\\your_path\\locations.csv', 'r') as f:

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

Leave a Comment