Writing binary to stdout
Christopher T King
squirrel at WPI.EDU
Wed Jun 23 15:00:02 EDT 2004
On Wed, 23 Jun 2004, Reinhold Birkenfeld wrote:
> Paul Watson wrote:
> > 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.
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?
More information about the Python-list
mailing list