2

Setup: python3.6 for windows in Cygwin (have to use Win version because of functionalities introduced in 3.5 and Cygwin is stuck at 3.4)

How to get \n new lines in buffer (stdout) output from a python script (instead of \r\n)? The output is a list of paths and I want to get one per line for further processing by other Cygwin/Windows tools.

All answers I've found so far are dealing with file writing and I just want to modify what is written to stdout. So far the only sure way to get rid of \r is piping results through sed 's/\\10//' which is awkward.

Weird thing is that even Windows applications fed with script output don't accept it with messages like:

Can't find file <asdf.txt
>

(note newline before >)

Supposedly sys.stdout.write is doing pure output but when doing:

sys.stdout.write(line)

I get a list of paths without any separation. If I introduce anything which resembles NL (\n, \012, etc.) it is automatically converted to CRLF (\r\n). How to stop this conversion?

CDspace
  • 2,639
  • 18
  • 30
  • 36
mikmach
  • 23
  • 2
  • Line endings differ in windows and unix (cygwin in this case). You have to manage that as you cross in and out of cygwin. Check out dos2unix package in the cygwin distribution to help. – AlG Mar 07 '17 at 21:09

1 Answers1

1

You need to write to stdout in binary mode; the default is text mode, which translates everything you write.

According to Issue4571 you can do this by writing directly to the internal buffer used by stdout.

sys.stdout.buffer.write(line)

Note that if you're writing Unicode strings you'll need to encode them to byte strings first.

sys.stdout.buffer.write(line.encode('utf-8')) # or 'mbcs'
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Thank you very much. In my case I've got to `sys.stdout.buffer.write((line+'\n').encode('utf-8'))`. For rest of program I store `line` without ornaments :) – mikmach Mar 08 '17 at 08:47