How to better control print and stdout?

Erik Max Francis max at
Sat Mar 15 00:27:32 CET 2003

Drew Smathers wrote:

> The program waits for the loop to end before printing anything.
> (If I omit the comma, this results in the program printing in sync
> with the loop but throws in the unwanted newline of course.)

What's happening is that sys.stdout is line buffered, so nothing prints
until you hit that newline.  (Note also that using the comma alternative
to print will print on one line, but will produce unexpected spaces; I'm
pretty sure this won't do what you expect because of the extra spaces
being produced, but I didn't check.)

Use sys.stdout.write (which only include spaces or newlines if you
include them explicitly) and call sys.stdout.flush after each print that
doesn't span a line.

 Erik Max Francis / max at /
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There is nothing wrong with sobriety in moderation.
\__/ John Ciardi
    Physics reference /
 A physics reference.

More information about the Python-list mailing list