Problems with sys.stout.flush()

H

Hendrik van Rooyen

"Joel Ross" <[email protected]>
To: <[email protected]>
Sent: Sunday, May 24, 2009 4:32 AM
Subject: Re: Problems with sys.stout.flush()

Mel said:
Joel said:
Rhodri James wrote: [ ... ]
Except that you still have the interesting issue that your environment
isn't responding to '\r' correctly, which worries me rather. Or did
you never test that?
Yeah I gave the "\r" a go and it kept printing out on a new line I will
look into it.

Are you running an Apple computer?

Mel.
No running a Linux fedora 9 box
Was the last character of your print command a comma?

- Hendrik
 
J

Joel Ross

AK said:
import time, sys

print "ONE",
sys.stdout.flush()
time.sleep(0.5)
print "\rTWO",
sys.stdout.flush()
time.sleep(0.5)

Running the command above prints out
ONE
TWO

but running

for i in range(10):
print "ONE",
time.sleep(0.2)

prints out
ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE

like Hendrik mentioned adding comma to the end prints out on one line.
But my problem problem wasn't printing on one line it was mathematical
each time I printed on one line it was appending the print before. Which
is all the above command does anyway. I had to apply some maths to solve
the issue I was having which was printing out a progress bar. I Got it
working though.

jross
 
D

Dennis Lee Bieber

Running the command above prints out
ONE
TWO

Both of the following work in a Windows command shell

-=-=-=-=-=-
C:\DOCUME~1\DENNIS~1>type t.py
import sys
import time
for i in range(10):
print "one\r",
time.sleep(1)
print "two\r",
time.sleep(1)

C:\DOCUME~1\DENNIS~1>t.py
two
C:\DOCUME~1\DENNIS~1>
-=-=-=-=-=-
C:\DOCUME~1\DENNIS~1>edit t.py

C:\DOCUME~1\DENNIS~1>type t.py
import sys
import time
for i in range(10):
print "\rone",
time.sleep(1)
print "\rtwo",
time.sleep(1)


C:\DOCUME~1\DENNIS~1>t.py
two

C:\DOCUME~1\DENNIS~1>
-=-=-=-=-=-

So... combining the "print ... ," with a "\r" at either the front or
end would have worked with your original computation (where you were
printing one * per percentage point).

The nice thing about this method is that you can (if you print
enough blanks too) overwrite the full line to "shrink" the bar if
needed.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top