469,951 Members | 2,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How to clear previous console output?

Hi, group,
May I ask a newbie question?

Given the below example:

for i in range(100):
print "%s%%" % i

I want to show the progress by print 1% to 100%. But I hope each
output will clear the previous output before print, so that I can see a
dynamic updated display of percentage, instead of print all percentages
one by one. How can I do that(what about on both windows and unix
platform)?

Thank you in advance for your expertise.

Best,

Cl

Jul 18 '05 #1
3 12814
(For console output) Use \r.

Example:

for i in range(100):
print '%s\r' % ' '*20, # clean up row
print '%d%%' % i, # note ending with comma
print

Note that it works only in real console window, not in simulated one (like
in WinIDE).
That is cross-platform.

HTH,
Mike

Newgene wrote:
I want to show the progress by print 1% to 100%. But I hope each
output will clear the previous output before print, so that I can see
a dynamic updated display of percentage, instead of print all


Jul 18 '05 #2
Try this class I use on both Linux and Windows
(console apps). See main program for example
usage.

HTH,
Larry Bates
class progressbarClass:
def __init__(self, finalcount, progresschar=None):
import sys
self.finalcount=finalcount
self.blockcount=0
#
# See if caller passed me a character to use on the
# progress bar (like "*"). If not use the block
# character that makes it look like a real progress
# bar.
#
if not progresschar: self.block=chr(178)
else: self.block=progresschar
#
# Get pointer to sys.stdout so I can use the write/flush
# methods to display the progress bar.
#
self.f=sys.stdout
#
# If the final count is zero, don't start the progress gauge
#
if not self.finalcount : return
self.f.write('\n------------------ %
Progress -------------------1\n')
self.f.write(' 1 2 3 4 5 6 7 8 9 0\n')
self.f.write('----0----0----0----0----0----0----0----0----0----0\n')
return

def progress(self, count):
#
# Make sure I don't try to go off the end (e.g. >100%)
#
count=min(count, self.finalcount)
#
# If finalcount is zero, I'm done
#
if self.finalcount:
percentcomplete=int(round(100*count/self.finalcount))
if percentcomplete < 1: percentcomplete=1
else:
percentcomplete=100

#print "percentcomplete=",percentcomplete
blockcount=int(percentcomplete/2)
#print "blockcount=",blockcount
if blockcount > self.blockcount:
for i in range(self.blockcount,blockcount):
self.f.write(self.block)
self.f.flush()

if percentcomplete == 100: self.f.write("\n")
self.blockcount=blockcount
return

if __name__ == "__main__":
from time import sleep
pb=progressbarClass(8,"*")
count=0
while count<9:
count+=1
pb.progress(count)
sleep(0.2)

pb=progressbarClass(100)
pb.progress(20)
sleep(0.2)
pb.progress(47)
sleep(0.2)
pb.progress(90)
sleep(0.2)
pb.progress(100)
print "testing 1:"
pb=progressbarClass(1)
pb.progress(1)

"Newgene" <ne*****@bigfoot.com> wrote in message
news:ce********@odbk17.prod.google.com...
Hi, group,
May I ask a newbie question?

Given the below example:

for i in range(100):
print "%s%%" % i

I want to show the progress by print 1% to 100%. But I hope each
output will clear the previous output before print, so that I can see a
dynamic updated display of percentage, instead of print all percentages
one by one. How can I do that(what about on both windows and unix
platform)?

Thank you in advance for your expertise.

Best,

Cl

Jul 18 '05 #3

"Mike Rovner" <mi**@nospam.com> wrote in message
news:ce**********@sea.gmane.org...
(For console output) Use \r.

Example:

for i in range(100):
print '%s\r' % ' '*20, # clean up row
print '%d%%' % i, # note ending with comma
print

Note that it works only in real console window, not in simulated one (like in WinIDE).
That is cross-platform.


If you do not want to erase everything, \10 in string inserts backspace
character, but blanking and rewriting everything may be easier.

tjr

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by AMeador | last post: by
1 post views Thread by cplusplusstudent | last post: by
3 posts views Thread by Jason | last post: by
4 posts views Thread by djc | last post: by
reply views Thread by Helge Lenuweit | last post: by
3 posts views Thread by Gureet Singh | last post: by
4 posts views Thread by =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post: by
27 posts views Thread by CarlosMB | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.