Alex Rivera | Logout

Make Python stop emitting a carriage return when writing newlines to sys.stdout

Asked 2012-04-04T22:45:21.000
13

I'm on Windows and Python is (very effectively) preventing me from sending a stand-alone '\n' character to STDOUT. For example, the following will output foo\r\nvar:

sys.stdout.write("foo\nvar")

How can I turn this "feature" off? Writing to a file first is not an option, because the output is being piped.

Edit
Report

1 Answer

-4

Add 'r' before the string:

sys.stdout.write(r"foo\nvar")

As expected, it also works for print.

answered 2012-04-06T22:55:29.117

Your Answer