[Tkinter-discuss] listdir

Vasilis Vlachoudis Vasilis.Vlachoudis at cern.ch
Thu Feb 15 05:24:10 EST 2018


Hi all,

I know that it it is not related to tkinter, but I had troubles in submitting in
the general python list.

I was trying to make my custom FileDialog to handle unicode filenames
and I stuck with the following behavior (python v2.7)

#path = "/home/bnv/Download"
path = u"/home/bnv/Download"
for fn in os.listdir(path):
    print(fn,type(fn))
    fullpath = os.path.join(path, fn)

If the "path" is defined as unicode I get the following output and error
(u'foo.py', <type 'unicode'>)
('Gewu\xccrzhalter.Part1.nc', <type 'str'>)
Traceback (most recent call last):
  File "foo.py", line 20, in <module>
    filename = os.path.join(path, fn)
  File "/usr/lib/python2.7/posixpath.py", line 73, in join
    path += '/' + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 5: ordinal not in range(128)

While if the "path" is defined as str. I get
('foo.py', <type 'str'>)
('Gewu\xccrzhalter.Part1.nc', <type 'str'>)

I don't understand why if the path is unicode the output of listdir is unicode the filenames
containing only ascii and str for the file with the non-ascii character.
While if the path is str, all output is in str

Thanks in advance
Vasilis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20180215/155a40a5/attachment.html>


More information about the Tkinter-discuss mailing list