Windows sys.stdout codepage / encoding

(Peter Suter) #1

The documentation on sys.stdout states:

Under Windows, if the stream is interactive (that is, if its isatty() method returns True ), the console codepage is used, otherwise the ANSI code page.

What is “the ANSI code page”? encodings.mbcs “Windows ANSI codepage”?

But testing in practice sys.stdio.encoding is actually cp1252 (if sys.stdio.atty() is False). Is it always cp1252?

What’s the advantage of / reason for switching to a different encoding than the console codepage?
How can other processes detect what encoding must be used? Won’t they typically assume the console codepage?


(Inada Naoki) #2

Because there is no console attached sometime. (e.g. pythonw.exe)

(Peter Suter) #3

Ah, that makes sense, thanks.
But this also happens with python.exe in a console when simply redirecting stdout to a file:
python.exe -c "print('ñ')" >example-file.ext
Or when redirecting stdout to another process:
python.exe -c "print('ñ')" |example-process.exe
Is the console not considered “attached” in these cases either?
What encoding should be expected in file example-file.ext / by process example-process.exe?
I always assumed it was the console codepage.