0

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?

tkbx
  • 15,602
  • 32
  • 87
  • 122
  • What's your input? A file? Can you paste the line of code that's throwing the exception? – Alastair McCormack Oct 29 '12 at 12:50
  • When your program exits it should contain a stack trace including a line number. Can you provide the exact line that is failing? – Alastair McCormack Oct 29 '12 at 13:22
  • @Fuzzyfelt It's running as a .app, just says something like `AppName has encountered an error and needs to close: SomeError: variable x referenced before assignment`. But I already know what's causing the error, I just needed to know how to fix it (jro answered) – tkbx Oct 29 '12 at 13:50

1 Answers1

2

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', (...)]
jro
  • 9,300
  • 2
  • 32
  • 37