How do I obtain the filenames' encoding after os.listdir?

Grzegorz Adam Hankiewicz gradha at
Wed Jun 4 11:50:03 CEST 2003


I'm using os.listdir() to read a directory. The function returns
a list of string objects:

In [3]: l = os.listdir(".")
In [7]: l[6]
Out[7]: 'm\xfasica.mid'
In [8]: l[6].decode("latin1")
Out[8]: u'm\xfasica.mid'

I want to transform the strings to unicode objects so I can use them
with pygtk, and the current process works ok for my machine. The
question is how do I know the encoding of the filenames?  Currently
I'm presuming latin1, but if somebody else uses a different encoding,
how do I know which one?

The documentation says string.decode can use as parameter "ignore"
or "replace", but using them only raises LookupErrors.

 Please don't send me private copies of your public answers. Thanks.

More information about the Python-list mailing list