How to designate sys.stdout for binary output in win32?
C.M. Lim
cmlim at ieee.org
Sun May 4 06:50:16 EDT 2003
I need to print strings as-is, control/escape chars et al, to sys.stdout in
a win32 python environment (in Unix, this would be trivial).
I found some sample code that uses msvcrt module to explicitly set the
BINARY mode, as follows:
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)
but on my system (Python2.1.1), sys.stdout is a PyShell.PseudoFile
instance, and does not have a fileno() method.
Other attempts at manipulating file descriptor 1 (usually a hard-coded fd
for a lot of systems) just show a "Bad file descriptor" error message.
To the gurus, how can I accomplish this?
Thanks in advance.
More information about the Python-list
mailing list