Printing Filenames with non-Ascii-Characters

"Martin v. Löwis" martin at v.loewis.de
Tue Feb 8 15:54:43 EST 2005


Marian Aldenhövel wrote:
>   dir = os.listdir(somepath)
>   for d in dir:
>      print d
>             
> The program fails for filenames that contain non-ascii characters.
> 
>   'ascii' codec can't encode characters in position 33-34:

I cannot reproduce this. On my system, all such file names print just
fine, in Python 2.3.4.

Are you using the Windows XP cmd.exe window to perform this experiment?
What (language) version of XP are you using?
What are your regional settings?
What is sys.stdout.encoding?
What is the result of locale.setlocale(locale.LC_ALL, "")?

> What I specifically do not understand is why Python wants to interpret the
> string as ASCII at all. Where is this setting hidden?

If this is in cmd.exe (or equivalent), Python
1. verifies that the output is indeed a terminal
2. if it is, determines what code page the terminal uses
3. tries to find a codec for this code page
4. if this fails, falls back to ASCII

In your case, step 4 must have happend. It's not clear to my why this
happened, unless you are using a Japanese version of XP (or some other
version for which Python does not have a codec).

> I am running Python 2.3.4 on Windows XP and I want to run the program on
> Debian sarge later.

In Linux, make sure that LANG is set to a value that allows Python to
infer the encoding of the terminal.

Regards,
Martin



More information about the Python-list mailing list