How to work around a unicode problem?

Chris Rebert clp2 at rebertia.com
Tue Jan 24 07:10:29 EST 2012


On Tue, Jan 24, 2012 at 3:57 AM,  <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.

After glancing at the docs, (untested):

with open(filepath) as f:
    image = pyexiv2.ImageMetadata.from_buffer(f.read())

Cheers,
Chris



More information about the Python-list mailing list