By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Printing w/o newlines inside loops - odd behavior

P: n/a
This is a difficult issue to search for, and Googling (and reviewing
the pertinent Python docs) has not found me anything useful. It's also
not a super important issue, but regardless of whether it's avoidable,
I still want to know what the heck is going on.

Doing multiple print statements inside a for loop, using the 'comma at
the end eats the newline' variant, results in zero output until the
loop completes its entire iteration.

def test():
print "testing:",
for x in range(10):
time.sleep(1)
print ".",
print "done."

Executing test() results in the behavior described above. If one
removes the commas after the first two print statements, it behaves
normally, with "testing" showing up right away, then a period on each
newline every second until the end.

I've tried various things--os.system('sleep 1') instead of
time.sleep(1), sys.stdout.write() instead of print-with-a-comma, and
other equally silly tactics, to no avail.

What's going on here?

Thanks,
Jeff

Sep 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jeffrey E. Forcier wrote:
Doing multiple print statements inside a for loop, using the 'comma at
the end eats the newline' variant, results in zero output until the
loop completes its entire iteration.


sys.stdout is line buffered. Put an intervening sys.stdout.flush() in
between your prints.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Substance is one of the greatest of our illusions.
-- Sir Arthur Eddington
Sep 13 '05 #2

P: n/a
Take a look at this recipe which is part of the latest
Python Cookbook. I think it will help you.

http://aspn.activestate.com/ASPN/Coo.../Recipe/299207

Larry Bates

Jeffrey E. Forcier wrote:
This is a difficult issue to search for, and Googling (and reviewing
the pertinent Python docs) has not found me anything useful. It's also
not a super important issue, but regardless of whether it's avoidable,
I still want to know what the heck is going on.

Doing multiple print statements inside a for loop, using the 'comma at
the end eats the newline' variant, results in zero output until the
loop completes its entire iteration.

def test():
print "testing:",
for x in range(10):
time.sleep(1)
print ".",
print "done."

Executing test() results in the behavior described above. If one
removes the commas after the first two print statements, it behaves
normally, with "testing" showing up right away, then a period on each
newline every second until the end.

I've tried various things--os.system('sleep 1') instead of
time.sleep(1), sys.stdout.write() instead of print-with-a-comma, and
other equally silly tactics, to no avail.

What's going on here?

Thanks,
Jeff

Sep 13 '05 #3

P: n/a
Erik: Thanks, that was it! Figured it was something along those lines,
but like I said, I didn't know specifically what was going on or what
to look for.

Larry: That recipe could be useful, thanks a lot :)

And thanks for the quick replies, too, you guys are awesome.

Regards,
Jeff

Sep 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.