Printing Filenames with non-Ascii-Characters

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


Marian Aldenhövel wrote:
>  > If you're printing to the console, modern Pythons will try to guess the
>  > console's encoding (e.g. cp850).
> 
> But it seems to have quessed wrong. I don't blame it, I would not know of
> any way to reliably figure out this setting.

It's actually very easy. Python invokes GetConsoleOutputCP() to find out
the encoding of the console (if the output is to a console, as
determined by isatty()).

> My console can print the filenames in question fine, I can verify that by
> simple listing the directory, so it can display more than plain ascii.
> The error message seems to indicate that ascii is used as target.

Yes, because that is the fallback.

> So if I were to fix this in sity.py to configure whatever encoding is
> actually used on my system, I could print() my filenames without explicitly
> calling encode()?

Yes. However, you cannot put a reasonable value in there, because
different parts of your system use different encodings. In particular,
the console likely uses CP 850, whereas the rest of your system
likely uses CP 1252.

> So wouldn't fixing site.py be the right thing to do? 

No. If they put CP850 into sitecustomize, Unicode in user interfaces
(menus etc) might be displayed as moji-bake, as the user interface
will likely assume CP1252, not CP850.

Regards,
Martin



More information about the Python-list mailing list