Refresh output line containing download percentage [duplicate]
I'm writing a script that has to download several files from the www. To make it more expressive I've just added a download hook to calculate the downloading percentage of the file that has being downloaded. Printing it out on the terminal produces a lot of output because I rewrite the same line each time the percentage counter is increm开发者_如何转开发ented, example:
1% - Downloading test.jpg
2% - Downloading test.jpg
... and so on
I'd like to obtain something like many bash scripts or programs (such as "apt-get" from ubuntu): refresh the line containing the percentage without have to write several times the same line containing the the updated percentage.
How can I do that? Thanks.
[edit] I'm using Python3.2 to develop the file downloader.
You need curses:
http://docs.python.org/howto/curses.html
Use \r
(carriage return) and write directly to the terminal with sys.stdout.write
:
import time
import sys
for i in range(101):
sys.stdout.write('%3d%%\r' % i)
time.sleep(.1)
Or a little more Python 3-ish:
import time
for i in range(101):
print('{:3}%'.format(i),end='\r')
time.sleep(.1)
精彩评论