469,915 Members | 2,635 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,915 developers. It's quick & easy.

print without newline "halts" program execution

Consider this short script:

---
from time import time, sleep

st = time()
print 'Start: %f, ' % st,
sleep(10)
sp = time()
print 'Stop: %f, Duration: %f' % (sp, (st - sp))
---

On my environment (Linux, py24), when run, Python first waits 10s, and
then produces the entire output. How, can I make it print first part
('Start: %f, '), then wait 10s, and then append (WITHOUT NEWLINE) that
second print statement?

I'm writting a script with lot of output which has to go on the same line,
but I don't want to wait for it to end to see output, I just want it to
print parts as it's finished with them.

Using sys.stdout.write() produces the same behavior, so what can I do?

Thanks a lot in advance.

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Apr 14 '06 #1
2 1546

On Apr 13, 2006, at 5:57 PM, Karlo Lozovina wrote:
Consider this short script:

---
from time import time, sleep

st = time()
print 'Start: %f, ' % st,
sleep(10)
sp = time()
print 'Stop: %f, Duration: %f' % (sp, (st - sp))
---

On my environment (Linux, py24), when run, Python first waits 10s, and
then produces the entire output. How, can I make it print first part
('Start: %f, '), then wait 10s, and then append (WITHOUT NEWLINE) that
second print statement?

I'm writting a script with lot of output which has to go on the same
line,
but I don't want to wait for it to end to see output, I just want it to
print parts as it's finished with them.

Using sys.stdout.write() produces the same behavior, so what can I do?
from time import time, sleep


Your problem is that the 'print' statement is sending the text to
sys.stdout, and sys.stdout is buffered.

There are other ways to do this, but try this:

from time import time, sleep
import sys

st = time()
print 'Start: %f,'% st,
sys.stdout.flush()
sleep(10)
sp = time()
print 'Stop: %f, Duration: %f' % (sp, (st-sp))
Jay P.

Apr 14 '06 #2
Jay Parlar <jp*****@cogeco.ca> wrote in
news:ma***************************************@pyt hon.org:
Your problem is that the 'print' statement is sending the text to
sys.stdout, and sys.stdout is buffered.
I thought it was something like this, but I couldn't find it in the docs :
(.
print 'Start: %f,'% st,
sys.stdout.flush()
sleep(10)
sp = time()
print 'Stop: %f, Duration: %f' % (sp, (st-sp))
This works excellent, *thank* you.
There are other ways to do this, but try this:


And for purely academical purposes, what are those other ways to do it?
I'm curious :).

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Apr 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Jane Austine | last post: by
6 posts views Thread by John | last post: by
10 posts views Thread by eyh5 | last post: by
reply views Thread by Michael Springer | last post: by
1 post views Thread by linuxnooby | last post: by
669 posts views Thread by Xah Lee | last post: by
2 posts views Thread by Ed Mittelstedt | last post: by
12 posts views Thread by filia&sofia | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.