Search code examples
pythonunicodewxpythonpy2app

Error when processing unicode string


I've got a wxPython app that I made into an app using py2app. It's worked fine for a while, and without changing anything that I know of, I suddenly get UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 2: ordinal not in range(128). I didn't change the file in any way. I didn't even rebuild the app, it's the same one I've been using, nothing different whatsoever. I've heard this can happen if "the terminal is not set to UTF-8", but this app doesn't use a terminal, and I haven't changed any settings in my terminal since it ran fine.

EDIT: I discovered the problem is that I have a line which does os.listdir(). The directory now contains a filename with a ü. Why does this error occur? Shouldn't it just change it from a string into a unicode type?


Solution

  • You need to provide a unicode string to os.listdir. When doing so, the filenames will be decoded using the filesystem's encoding.

    import os
    >>> os.listdir(u'c:\\')
    [u'$Recycle.Bin', u'Config.Msi', u'Documents and Settings', u'hiberfil.sys', (...)]