Printing/updating output to the screen

D

Daniel Pryde

Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel
 
L

Larry Bates

Daniel,

Here is an example progressbar class that
I use in my programs.

-Larry

class progressbarClass:
def __init__(self, finalcount):
import sys
self.finalcount=finalcount
self.blockcount=0
self.block="*"
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):
if (count > self.finalcount): count=self.finalcount
if (self.finalcount != 0) :
percentcomplete=int(round(100*count/self.finalcount))

if (percentcomplete < 1):
percentcomplete=1
else:
percentcomplete=100

blockcount=int(percentcomplete/2)
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)
 
D

Daniel Ehrenberg

Daniel Pryde said:
Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel

Put a comma after it. For example:

print "hi",

This, however, also prints a space. If that's not what you want, do the following:

from sys import stdout
stdout.write("hi")

That won't append a space or a newline.


Another Daniel
 
J

Josiah Carlson

Daniel said:
Hi there. I hope this isn't a stupid question to ask, but does anyone
know how to print out a string without moving to a new line each time
and simply updating the first line. An example would be, if I wanted to
have a percentage progress counter that was constantly updating. I'm
unsure how to do this without printing to a brand new line. Any help
would be greatly appreciated. Thanks.

Daniel

try this...

import time

for i in xrange(101):
print i, '\r',
time.sleep(.1)

- Josiah
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top