Printing LF with Python 3 for Windows stdout

How to get \n printed on stdout on Windows? This code works in Python 2, but not with 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#") 
+5
source share
1 answer

Python 3 already configures standard binary I / O, but it has its own I / O implementation that wraps a new line. Instead of using print , which requires a text file, you can manually call sys.stdout.buffer.write to use the binary mode of BufferedWriter . If you need to use print , you will need a new text input / output wrapper that does not use universal newline characters. For instance:

 stdout = open(sys.__stdout__.fileno(), mode=sys.__stdout__.mode, buffering=1, encoding=sys.__stdout__.encoding, errors=sys.__stdout__.errors, newline='\n', closefd=False) 

Since closefd is false, closing this file will not close the original sys.stdout file sys.stdout . You can use this file explicitly via print("#\n#", file=stdout) or replace sys.stdout = stdout . The original is available as sys.__stdout__ .

Background

The Python 3 io module was designed to provide cross-platform and cross-implementation (CPython, PyPy, IronPython, Jython) specifications for all file objects in terms of the abstract base classes RawIOBase , BufferedIOBase , and TextIOBase . It includes a reference implementation of pure Python in the _pyio module. The common denominator for implementing raw io.FileIO is a set of low-level POSIX system calls such as read and write , which eliminates the CRT stdio inconsistency problem. On Windows, the POSIX layer is just a low CRT I / O, but at least limited to the quirks of one platform.

One of quirks Windows has non-standard text and binary modes in its POSIX I / O layer. Python addresses this by always using binary mode and calling setmode on file descriptors stdio 1 .

Python can avoid using Windows CRT for I / O by implementing the WinFileIO registered subclass of RawIOBase . There a patch is proposed for this in question 12939 . Another example is the win_unicode_console module, which implements the WindowsConsoleRawReader and WindowsConsoleRawWriter classes.


<sub> 1. This caused problems for programs that embed Python and expect stdio to use text mode by default. For example, in binary mode, printing wide-character strings no longer goes to char , as in ANSI text mode, and, of course, does not print using WriteConsoleW , as it would in UTF-16 text mode. For instance:

 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') wide 5 

sub>

+5
source

Source: https://habr.com/ru/post/1241295/


All Articles