5

Possible Duplicate:
Python Progress Bar

I am running a for loop 10,000 times, but inside the loop there are a lot of calculations going on. I would like to print out a progress message to the console informing me how far along in the loop the program is how much longer I might have to wait.

the basic loop is

n = 10000 
for i in range(n):
   do_stuff_method()
   if(i%100 == 0):
      print (float(i)/n)*100,

This prints out a percentage message on the same line, but the problem is that the next thing that I print out is also printed out on the same screen. That, and since there are 99 prinouts, the console gets pretty wide and there is a lot of scrolling across.

What I would really like is for the console to print out the current % done, and an estimated time to finsih on the one line replace that which had been previously printed, so there doesn't have to be a lot scrolling.

Can this be done?

Cheers, Davy

1
  • In addition, a package exists for python to do this called... Progress Bar. Check it out from pypi: pypi.python.org/pypi/progressbar/2.2 Commented Jul 25, 2012 at 15:00

1 Answer 1

8

In your case you can do it simply by changing your print line to be:

print "\r{0}".format((float(i)/n)*100),

Or you can try it like this instead of print:

sys.stdout.write("\r{0}".format((float(i)/n)*100))
sys.stdout.flush()
Sign up to request clarification or add additional context in comments.

8 Comments

I just tested this. It prints each value on a new line.
you can try that other solution I put in the answer, both of them work for me
yep.I tried with and without the comma, and with and without the \r but they all just print either on the same line, or on a new line. I was looking for something that prints the new % value over the old % value.
I tried the second answer also. Still each value on new line. Weird. Could it be something to do with my console. It's the console inside eclispe on a mac if that makes a difference
can't really answer you on that but you can try running it from your regular terminal
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.