[Python-de] glob stirbt mit unicode error

Peter Otten __peter__ at web.de
Di Mär 6 19:16:33 CET 2012


robert at redcor.ch wrote:

> Hoi zäme,
> 
> auf einer linux-box versuche ich ein directory listing abzuarbeiten.
> Dazu nutze ich in etwa folgendes:
> 
> files += glob.glob(ospath + '/*pdf')
> das verursacht folgenden Fehler:
> 
>   File "/usr/local/lib/python2.6/glob.py", line 16, in glob
>     return list(iglob(pathname))
>   File "/usr/local/lib/python2.6/glob.py", line 43, in iglob
>     yield os.path.join(dirname, name)
>   File "/usr/local/lib/python2.6/posixpath.py", line 70, in join
>     path += '/' + b
>   File "/usr/local/lib/python2.6/encodings/utf_8.py", line 16, in decode
>     return codecs.utf_8_decode(input, errors, True)
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 14:
> invalid continuation byte
> 
> das Problem ist wohl, dass einige der Dateien von Windows mit umlaufen
> geschrieben wurden.
> Das python-Systemencoding is 'utf-8'
> 
> was kann ich da machen um das Problem zu beheben?
> danke für eure Typs.

Ist ospath ein Unicodestring? Wenn ja, hast du mal versucht, durchgehend 
bytestrings zu verwenden? Also

if isinstance(ospath, unicode):
    ospath = ospath.encode("utf-8")
files += glob.glob(ospath + '/*pdf')




Mehr Informationen über die Mailingliste python-de