Eryk Sun <eryk...@gmail.com> added the comment: > # Power Shell 6 (use cp65001 by default) > PS C:¥> python3 -c "print('おはよう')" > ps.txt
PowerShell standard I/O redirection is different from any shell I've ever used. In this case, it runs Python with StandardOutput set to a handle for a pipe instead of a handle for the file. It decodes Python's output using whatever encoding is configured for input and re-encodes it with whatever encoding is configured for output. To see what Python is actually writing, try using Start-Process with StandardOutput redirected to the file. For example: Start-Process -FilePath python3.exe -ArgumentList "-c `"print('おはよう')`"" -NoNewWindow -Wait -RedirectStandardOutput ps.txt > # cmd.exe > C:¥> chcp 65001 > C:¥> python3 -c "print('おはよう')" > cmd.txt CMD uses simple redirection, like every other shell I've ever used. It runs python3.exe with a handle for the file as its StandardOutput. So "cmd.txt" contains exactly what Python writes. > * TextIOWrapper tries `os.device_encoding(1)` > * `os.device_encoding(1)` use GetConsoleOutputCP() without checking stdout is > console No, _Py_device_encoding returns None if the isatty(fd) is false, i.e. for a pipe or disk file. In this case, TextIOWrapper defaults to the encoding from locale.getpreferredencoding(). The current preferred encoding is the system ANSI codepage from GetACP(). Changing the default to UTF-8 would be disruptive. You can use UTF-8 mode (i.e. -X utf8). Or, to override just stdin, stdout, and stderr, set the environment variable "PYTHONIOENCODING=utf-8". > In the example above, a console is attached when python is called from > Power Shell 6, but it is not attached when python is called from > cmd.exe. In both cases the Python process inherits the console of the parent shell. The only way to run python.exe without a console is to use the CreateProcess creation flag DETACHED_PROCESS. > There is a relating issue: UTF-8 mode doesn't override > stdin,stdout,stderr encoding when console is attached. It works for me. For example, testing with stdout redirected to a pipe: C:\>python -c "import sys;print(sys.stdout.encoding)" | more cp1252 C:\>python -X utf8 -c "import sys;print(sys.stdout.encoding)" | more utf-8 ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue37275> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com