How to print to stdout in binary

pehr anderson pehr at pehr.net
Thu Jul 6 22:29:34 EDT 2000


What platform are you developing on?
under linux I did the following in a file "t.py":

	import sys
	x = open("python.prc", "rb").read()
	sys.stdout.write(x)

[pehr at morseall python]$ python t.py  > t
[pehr at morseall python]$ diff python.prc t

I didn't get any differences when I did this. 
Would you try it and see how it goes?

	-pehr


brianbird at my-deja.com wrote:
> 
> I've seen similar questions to this posted in other language forums,
> but I've not seen anything for Python:
> 
> Does anyone know how to tell python to print the sys.stdout in binary
> rather than plain text. It's mode is set as 'w' and I can't seem to
> change it.
> 
> I'm experimenting with a cgi script which I want to output an image to
> the browser so that the HTML file can have:
> <IMG SRC="test.cgi">
> and the resulting output of the script is an image.
> I've currently got:
> 
> f = open("test.jpg","rb")
> bytes = f.read()
> f.close()
> sys.stdout.print(bytes)
> 
> but I want the output to print in binary mode so that the image can be
> displayed correctly. If I was printing to another file I could open it
> with:
> 
> f2 = open("test2.jpg","wb")
> f2.write(bytes)
> f2.close()
> 
> so is there any way to do this with stdout?
> 
> Thanks
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.



More information about the Python-list mailing list