Printing binary output on sys.stdout under Windows

C.M. Lim cmlim at ieee.org
Sat May 3 07:14:01 EDT 2003


I am trying to print binary output on sys.stdout in the Windows 
environment.  Used normally, sys.stdout.write would 'truncate' the output 
string on any NULL characters in the output string.

I found a code snippet that goes:

    #
    #	Print in binary format
    #
    def binaryPrint(s) :
	import sys
	try :
	    #Make sure we use binary mode if we are running on windows
	    import msvcrt, os
	    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
	except :
	    pass
	#use stdout instead of print because print will add an extra new line   
character at the end
	sys.stdout.write(s)



except that it did not work for me.  On further checking, it seems like
sys.stdout is a PyShell.PseudoFile instance and does not have the fileno()
method.

Was guessing that stdout may be using file descriptor 1 (or 2) like many 
other languages, and trying to manipulate it thusly, but that did not pan 
out either...

I am sure someone has done this before.  Please help.  I am using 
Python2.1.1 from www.python.org, if that makes a difference.




More information about the Python-list mailing list