print()

Dave Angel davea at ieee.org
Sat Oct 17 05:39:38 CEST 2009


mattia wrote:
> Is there a way to print to an unbuffered output (like stdout)? I've seen 
> that something like sys.stdout.write("hello") works but it also prints 
> the number of characters!
>
>   
What the other responses (so far) didn't address is your comment about 
"prints the number of characters."

You're presumably testing this in the interpreter, which prints extra 
stuff.  In particular, it prints the result value of any expressions 
entered at the interpreter prompt.  So if you type

sys.stdout.write("hello")

then after the write() method is done, the return value of the method 
(5) will get printed by the interpreter.

Either put the statement in a real script, or do the following trick to 
convince yourself:

dummy = sys.stdout.write("hello")

DaveA



More information about the Python-list mailing list