os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Nov 17 10:08:27 CET 2006

In <mailman.294.1163721712.32031.python-list at python.org>, Jean-Paul
Calderone wrote:

>>How would you propose listdir should behave?
> Umm, just a wild guess, but how about raising an exception which includes
> the name of the file which could not be decoded?

Suppose you have a directory with just some files having a name that can't
be decoded with the file system encoding.  So `listdir()` fails at this
point and raises an exception.  How would you get the names then? Even the
ones that *can* be decoded?  This doesn't look very nice:

path = u'some path'
    files = os.listdir(path)
except UnicodeError, e:
    files = os.listdir(path.encode(sys.getfilesystemencoding()))
    # Decode and filter the list "manually" here.

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list