0

Running the following code on Windows 10:

import subprocess

if __name__ == "__main__":
    p = subprocess.run(["python","-c", "print('\\n', end='');"], capture_output=True)
    print(p.stdout)

Results in:

b'\r\n'

This is a problem because if an external program prints \r\n it's read as \r\r\n.

Is it possible to prevent Python automatically converting \n to \n\r while writing on sys.stdout? Does this behavior actually comes from Windows itself and the standard output implementation? How to get the "raw" output without the added carriage return?


Note: this is not due to subprocess. Another way to reproduce the issue is as follow:

# print.py
print("\n", end="")
python print.py >> out.txt
with open("out.txt", "rb") as f:
    print(f.read())
    # => b'\r\n'
Delgan
  • 18,571
  • 11
  • 90
  • 141

0 Answers0