如何在 Windows 上将
\n
打印到标准输出?此代码适用于 Python 2,但不适用于 Python 3:
# set sys.stdout to binary mode on Windows
import sys, os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# the length of testfile created with
# python test_py3k_lf_print.py > testfile
# below should be exactly 4 symbols (23 0A 23 0A)
print("#\n#")
Python 3 已经在二进制模式下配置了标准 I/O,但它有自己的 I/O 实现来进行换行转换。您可以手动调用
print
以使用二进制模式 sys.stdout.buffer.write
,而不是使用需要文本模式文件的 BufferedWriter
。如果您需要使用 print
,那么您将需要一个不使用通用换行符的新文本 I/O 包装器。例如:
stdout = open(sys.__stdout__.fileno(),
mode=sys.__stdout__.mode,
buffering=1,
encoding=sys.__stdout__.encoding,
errors=sys.__stdout__.errors,
newline='\n',
closefd=False)
由于
closefd
为 false,关闭此文件不会关闭原始 sys.stdout
文件描述符。您可以通过 print("#\n#", file=stdout)
显式使用此文件,或替换 sys.stdout = stdout
。原件可作为 sys.__stdout__
获得。
背景
Python 3 的
io
模块旨在根据抽象基类 RawIOBase
、BufferedIOBase
和 为所有类文件对象提供跨平台和跨实现(CPython、PyPy、IronPython、Jython)规范TextIOBase
。它在 _pyio
模块中包含一个参考纯 Python 实现。原始 io.FileIO
实现的共同点是一组低级 POSIX 系统调用,例如 read
和 write
,这消除了 CRT stdio 不一致的问题。在 Windows 上,POSIX 层只是 CRT 的低 I/O 层,但至少这仅限于单个平台的怪癖。
Windows 的怪癖之一是其 POSIX I/O 层中具有非标准文本和二进制模式。 Python 通过始终使用二进制模式并在 stdio 文件描述符
1上调用
setmode
来解决此问题。
Python 可以通过实现
WinFileIO
的 RawIOBase
注册子类来避免使用 Windows CRT 进行 I/O。 issue 12939 中有一个针对此问题的建议补丁。另一个例子是 win_unicode_console 模块,它实现了 WindowsConsoleRawReader
和 WindowsConsoleRawWriter
类。
1. 这给嵌入Python并期望stdio使用默认文本模式的程序带来了问题。例如,在二进制模式下,打印宽字符字符串不再像在 ANSI 文本模式下那样转换为
char
,并且肯定不会像在 UTF-16 文本模式下那样使用 WriteConsoleW
进行打印。例如:
Python 2.7.10 (default, May 23 2015, 09:44:00)
[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os, msvcrt, ctypes
>>> ctypes.cdll.msvcr90.wprintf(b'w\x00i\x00d\x00e\x00\n\x00')
wide
5
>>> msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
16384
>>> ctypes.cdll.msvcr90.wprintf(b'w\x00i\x00d\x00e\x00\n\x00')
w i d e
5