How to work around a unicode problem?

T

tinnews

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.
 
C

Chris Rebert

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
 
P

Peter Otten

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.
 
T

tinnews

Chris Rebert said:
After glancing at the docs, (untested):

with open(filepath) as f:
image = pyexiv2.ImageMetadata.from_buffer(f.read())
Excellent, worked perfectly (after I spotted I had another variable f).

Thank you.
 
T

tinnews

Peter Otten said:
Try passing a unicode filename. A quickfix:

filepath = filepath.decode(sys.getfilesystemencoding())
image = pyexiv2.ImageMetadata(filepath)
.... and this solution works too, thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top