473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 1670

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
6
by: John | last post by:
Hi I have this code as below that first runs a report and then an update query to set a flag so these records are not included in the report next time. Private Sub Command2_Click()...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
0
by: Michael Springer | last post by:
I have written a program that talks to a USB device. When I run the program under WinXP, it works flawlessly. When I run it under Win98se, it freezes at a particular point unless I add a...
22
by: stephen | last post by:
I have created an order form that users javascript to create a new html document when the customers clicks the "print page" button. Once the new document has been created it then prints the...
1
by: linuxnooby | last post by:
HI I am creating a tkinter app. example tkMessageBox.showinfo("Window Text", "A short message") print "blah" The execution of the application halts when the message box is
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
by: Ed Mittelstedt | last post by:
Hello, I've been wrestling with this problem for almost 4 months now. I am trying to run two instances of the same java server app on 1 linux box. Basically they are listening on seperate...
12
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.