Confusing problem >> FileNotFoundError: [Errno 2] No such file or directory:

Question:

This problem puzzled me.
Maybe the problem is in the code, I hope you take a look

        with open(training_images_labels_path,'r') as file:
            lines = file.readlines()

He says that the file does not exist

FileNotFoundError: [Errno 2] No such file or directory: '\Desktop\project\data\generated\training_images_labels.txt'

Though the file exists

I need solutions

Asked By: salaheddine97

||

Answers:

Looks like its a windows path you are working and i believe path really thrown in the error is wrong when compared to the actual where the txt file resides.. just cross check once, if that’s the case try to pass the correct path in to the variable “training_images_labels_path”

Answered By: hariram manohar

If it says that the file does not exist though the file exists, it means the path has been not given properly. Try giving the path correctly.

Method 1:

Giving correct path 'C:\Users\Public\Desktop\project\data\generated\training_images_labels.txt' or

'C:\Users\<insert your username>\Desktop\project\data\generated\training_images_labels.txt' is your path if I guess correctly

Method 2:

Using os module ( Recommended )

mydir = 'C:/Users/Public/Desktop/project/data/generated'
myfile = 'training_images_labels.txt'
training_images_labels_path = os.path.join(mydir, myfile)

with open(training_images_labels_path,'r') as file:
    lines = file.readlines()

Method 3:

You can also try changing the working directory to the location where your data is present. ie Desktop>project>data>generated here and open the file with file name. ie

with open('training_images_labels.txt','r') as file:
        lines = file.readlines()
Answered By: Manojk07

Can you tell how you created this path.Some advise.
use path separator library to generate path to avoid this error.

training_images_labels_path

further try to navigate parent directory using python and print pth.may be some new line or linux/windwos convereted path or other special character in path. navigating parent directory and listing will solve

if still not solve try to navigatep parent-parent dir and print path

try hard

Answered By: Notepad

I had the same problem with importing an excel file, which of course exists in the same directory with my .py file. The chosen solution above did not help me, and actually I didn’t understand those three methods, as I am working on mac OS.

This method worked for me: in Spyder, I usually run the file by pressing the keys "Shift + Enter", which in this case made the problem. So, my solution was, to press on the "Run file" button (or the fn+F5 key) instead.

Maybe someone wants to explain the difference.

Answered By: gloschtla

Watch your path if its correct or not. I had the same problem and it turned out i didnt had the good path set

Answered By: D3F4ULT.-

Something that I have realized and I do not know if it is your case, is that if you create a file and put for example: "device.txt", in the python code to open it you must put for example: "file = open(" device.txt.txt", "r")" because it doesn’t take into account the ".txt" written in the file name, but the format itself. and if you don’t want to write it that way, rename the file where you have it saved, from "device.txt" to "device".

Answered By: Ronald Fuentes
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.