Writing binary to stdout

P

Paul Watson

How can I write lines to stdout on a Windows machine without having '\n'
expanded to '\r\n'.

I need to do this on Python 2.1 and 2.3+.

I see the msvcrt.setmode function. Is this my only path? Is it valid to
change the mode of stdout? The file.newlines is not writable.
 
R

Reinhold Birkenfeld

Paul said:
How can I write lines to stdout on a Windows machine without having '\n'
expanded to '\r\n'.

I need to do this on Python 2.1 and 2.3+.

I see the msvcrt.setmode function. Is this my only path? Is it valid to
change the mode of stdout? The file.newlines is not writable.

What about opening the file in binary mode? This should give you control
over the line endings.

Reinhold
 
C

Christopher T King

What about opening the file in binary mode? This should give you control
over the line endings.

Believe it or not, open('CON:','wb') actually works under WinXP. It's
amazing that this relic from DOS is still around. Though unportable (the
Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.

Optimally, you'd want to use something like C's freopen to re-open
sys.stdout in binary mode, but I can't find anything like it under the os
module. Does Python not have this ability?
 
C

Christopher T King

Believe it or not, open('CON:','wb') actually works under WinXP. It's
amazing that this relic from DOS is still around. Though unportable (the
Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.

I correct myself. 'CON:' isn't really stdout, but rather the console. So
this won't work if you connect stdout to something.
 
P

Paul Watson

Christopher T King said:
Believe it or not, open('CON:','wb') actually works under WinXP. It's
amazing that this relic from DOS is still around. Though unportable (the
Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.

Optimally, you'd want to use something like C's freopen to re-open
sys.stdout in binary mode, but I can't find anything like it under the os
module. Does Python not have this ability?

This will be a command to run a python script from inside another tool.
It -must- write to stdout. Also, Cygwin is on the machine.

Even when I try to use low-level I/O it does CRLF interpretation. What am I
doing wrong?

pwatson [ watsonp:/cygdrive/c/src/projects/pwatson/bin ] 22
$ cat ./wb.py
#! /usr/bin/env python
import sys
import os

try:
import msvcrt
f = os.open(1, 'wb')
except:
f = sys.stdout

f.write("now\n")
f.close()

sys.exit(0)
pwatson [ watsonp:/cygdrive/c/src/projects/pwatson/bin ] 23
$ ./wb.py >jjj
pwatson [ watsonp:/cygdrive/c/src/projects/pwatson/bin ] 24
$ od -c jjj
000000 6e 6f 77 0d 0a
n o w \r \n
000005
 
C

Chris King

Paul said:
Even when I try to use low-level I/O it does CRLF interpretation. What am I
doing wrong?

Are you using Cygwin? It will automatically CRLFify anything it thinks
should be (I'm not sure what the exact critera are); this has bit me
before, too. There's an option to disable it in Cygwin setup. This is
being passed on to the script because the stdout redirections are done
in Cygwin, not in Python.
 
P

Paul Watson

Chris King said:
Are you using Cygwin? It will automatically CRLFify anything it thinks
should be (I'm not sure what the exact critera are); this has bit me
before, too. There's an option to disable it in Cygwin setup. This is
being passed on to the script because the stdout redirections are done
in Cygwin, not in Python.

The following appears to work under 2.1 in Cygwin and 2.3.4 under a DOS box.
I did not change any Cygwin setting. Is there anything undesireable in the
code below? The intent is to say if this is running on Windows, set stdout
to binary mode.

Am I safe in assuming that 'import msvcrt' will fail on all other machines?
Is there a better way?

$ cat ./wb.py
#! /usr/bin/env python
import sys
import os

try:
import msvcrt
msvcrt.setmode(1, os.O_BINARY)
except:
pass

sys.stdout.write("now\n")
print "and then"

$ ./wb.py >jjj

$ od -c jjj
000000 6e 6f 77 0a 61 6e 64 20 74 68 65 6e 0a
n o w \n a n d t h e n \n
00000d
 
C

Christos TZOTZIOY Georgiou

Believe it or not, open('CON:','wb') actually works under WinXP. It's
amazing that this relic from DOS is still around. Though unportable (the
Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.

/dev/stdout I believe is a Linux device. /dev/tty is the traditional
Unix device for reading/writing to the current "tty".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top