How to work around a unicode problem?
Peter Otten
__peter__ at web.de
Tue Jan 24 07:43:01 EST 2012
tinnews at isbd.co.uk wrote:
> I have a small python program that uses the pyexiv2 package to view
> exif data in image files.
>
> I've hit a problem because I have a filename with accented characters
> in its path and the pyexiv2 code traps as follows:-
>
> Traceback (most recent call last):
> File "/home/chris/bin/eview.py", line 87, in <module>
> image = pyexiv2.ImageMetadata(filepath)
> File "/usr/lib/python2.7/dist-packages/pyexiv2/metadata.py", line
> 65, in __init__
> self.filename = filename.encode(sys.getfilesystemencoding())
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 38: ordinal not in range(128)
>
> Without digging deep into pyexiv2 is there any way I can work around
> this error? The accented characters aren't in the filename itself,
> they're in the directory path. I.e. it's:-
>
> ./1977/04 April/#09 - Monaco, inc. Musée de Poupée/p77_08_011.jpg
>
> I could of course remove the accents but I'd much prefer not to do so.
Try passing a unicode filename. A quickfix:
filepath = filepath.decode(sys.getfilesystemencoding())
image = pyexiv2.ImageMetadata(filepath)
If you are using os.listdir() or glob.glob() to produce the filepath -- they
will return unicode filenames if you invoke them with a unicode argument.
More information about the Python-list
mailing list