My question is similar to part of this one: Python says it can't find the file I am trying to rename even though it can read the file name when OP asks about it being weird that Python can see the file (how else would it print its filename in the error message?). That part was never resolved in the answers, and the answers don't help my case, so I'm starting a new question.
In my case, I'm just trying to open all the files in a folder and do something with them (they are .tif files and I doing some image processing with them). The .py script sits in the same folder as the folder containing the files, and I'm using the absolute path to tell it where to look. Weirder still is the fact that I can do this successfully with the files in one folder, so I know the code works, but when I try the same code snippet directing it to another folder ("directory2") it throws this error. I'm not asking it to find a specific file, yet it finds it. So does the fact that it is being directed correctly, and can see the file, but not able to open it help anyone help me with this problem please?
directory2 = "folders_absolute_path"
your textfor filename in os.listdir(directory2):
I get this:
FileNotFoundError: [Errno 2] No such file or directory: 'name_of_file.tif'
Here are the details:
26 for filename in os.listdir(directory2):
---> 28 imarray = np.array(Image.open(filename))
30 imarray[imarray == 1] = 19
32 imarray[imarray == 2] = 20
File ~\.conda\envs\Image_analysis\lib\site-packages\PIL\Image.py:3131, in open(fp, mode, formats)
3128 filename = fp
3130 if filename:
-> 3131 fp = builtins.open(filename, "rb")
3132 exclusive_fp = True
3134 try:
FileNotFoundError: [Errno 2] No such file or directory: 'name_of_file.tif'`
I've checked there are no permission issues with the files or folders. It doesn't seem to be a code problem because as mentioned above, it works! I'm all out of ideas.