[Tkinter-discuss] listdir

Michael Lange klappnase at web.de
Thu Feb 15 14:32:11 EST 2018


Hi,

On Thu, 15 Feb 2018 10:24:10 +0000
Vasilis Vlachoudis <Vasilis.Vlachoudis at cern.ch> wrote:

> 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

I think this is because with "foo.py" no UnicodeError occurs and so the
str->unicode conversion succeeds. 

I can only guess why that UnicodeError occurs though, this doesn't happen
here. When I do $ export LANG=C before starting the python interpreter
however I get this same error as you (by default I have UTF-8 here).
So I think maybe your system default encoding is set to something that
does not support the non-ascii character in the filename which causes the
problem. This might also explain why the default tk file dialogs do not
seem to work for you (they work well here with unicode filenames).
Just a guess, of course.

Best regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

If a man had a child who'd gone anti-social, killed perhaps, he'd still
tend to protect that child.
		-- McCoy, "The Ultimate Computer", stardate 4731.3


More information about the Tkinter-discuss mailing list