Closed acroloid closed 7 months ago
Hi It's a simple and clear exception. It's because this file path doesn't exist.
For example, my computer doesn't have the file, so I can't open it either:
>>> path = r'C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg'
>>> f = open(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Jalif\\Desktop\\delfot\\foto\\3\\foto\\IMG_20231018_115810.jpg'
LeoHsiao1 Thank you for your answer! I just have a lot of subfolders and a lot of photos. And this is just one example of an error, in fact there are thousands of such errors. Your answer probably works for one image, but I can't use it for thousands of variants. I use os.walk and os.path.join to get the absolute path for the file and, judging by, print this path to each image is correct. But why does pyexiv2 give a path error? How do I fix this?
Common causes of FileNotFoundError:
>>> r'C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg'
'C:\\Users\\Jalif\\Desktop\\delfot\\foto\\3\\foto\\IMG_20231018_115810.jpg'
If the problem is not solved, you can try executing the following code and tell me what the output is
import pyexiv2
import os
import pathlib
for root, dirs, files in os.walk(pathlib.Path.cwd()):
for file_name in files:
if file_name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
image_path = os.path.join(root, file_name)
print(image_path)
print(os.path.exists(image_path)) # Test if the file exists
img = pyexiv2.Image(image_path) # Test if the file can be opened by pyexiv2.
print(img)
break
break
C:\Users\Jalif\Desktop\delfot\foto\1\foto\IMG_20231018_115715.jpg
True
Traceback (most recent call last):
File "C:\Users\Jalif\Desktop\delfot\foto\test.py", li
ne 11, in
File "C:\Python\Python38\lib\site-packages\pyexiv2\core.py", line 15, in ini t self.img = exiv2api.Image(filename.encode(encoding)) RuntimeError: C:\Users\Jalif\Desktop\delfot\foto\1\foto\IMG_20231 018_115715.jpg: Failed to open the data source: No such file or directory (errno = 2)
But I decided to put the folder in the root of the C drive to check the problem in the folder path, and that's what happened, it seems to work and there is some kind of problem in getting the path to the folder on the desktop
C:\delfot\foto\1\foto\IMG_20231018_115715.jpg True <pyexiv2.core.Image object at 0x00000000025A2B80> C:\delfot\foto\2\foto\IMG_20231018_115715.jpg True <pyexiv2.core.Image object at 0x00000000028D8850> C:\delfot\foto\3\foto\IMG_20231018_115715.jpg True <pyexiv2.core.Image object at 0x00000000025A2C70>
Sounds like a file permission issue. Excuse me, could you try to execute the following code?
path = r'C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg'
print(os.path.exists(path)) # Test if the file exists
f = open(path) # Test if the file can be opened by open()
img = pyexiv2.Image(path) # Test if the file can be opened by pyexiv2
True
Traceback (most recent call last):
File "test.py", line 6, in <module>
img = pyexiv2.Image(path) # Test if the file can be opened by pyexiv2
File "C:\Python\Python38\lib\site-packages\pyexiv2\core.py", line 15, in __init__ self.img = exiv2api.Image(filename.encode(encoding))
RuntimeError: C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg: Failed to open the data source: No such file or directory (errno = 2)
I think it's a problem with encoding the path to the file. Some character in the path may contain a simbol that cannot be read or something like that.
If the file path contains unicode characters, then you need to declare the encoding format. https://github.com/LeoHsiao1/pyexiv2/blob/master/docs/Tutorial.md#class-image
Or you can open the image with the open() function and then call pyexiv2.Imagedata to open the image. https://github.com/LeoHsiao1/pyexiv2/blob/master/docs/Tutorial.md#class-imagedata
Hi, I use this code to remove exif tags
but I get this error: Error deleting exif tags for an image C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg: C:\Users\Jalif\Desktop\delfot\foto\3\foto\IMG_20231018_115810.jpg: Failed to open the data source: No such file or directory (errno = 2)