How to write a download progress indicator in Pyth

2019-03-07 18:10发布

I am writing a little application to download files over http (as, for example, described here).

I also want to include a little download progress indicator showing the percentage of the download progress.

Here is what I came up with:

    sys.stdout.write(rem_file + "...")    
    urllib.urlretrieve(rem_file, loc_file, reporthook=dlProgress)

    def dlProgress(count, blockSize, totalSize):
      percent = int(count*blockSize*100/totalSize)
      sys.stdout.write("%2d%%" % percent)
      sys.stdout.write("\b\b\b")
      sys.stdout.flush()

Output: MyFileName... 9%

Any other ideas or recommendations to do this?

One thing that's somewhat annoying is the blinking cursor in the terminal on the first digit of the percentage. Is there a way to prevent this? Is there a way to hide the cursor?

EDIT:

Here a better alternative using a global variable for the filename in dlProgress and the '\r' code:

    global rem_file # global variable to be used in dlProgress

    urllib.urlretrieve(rem_file, loc_file, reporthook=dlProgress)

    def dlProgress(count, blockSize, totalSize):
      percent = int(count*blockSize*100/totalSize)
      sys.stdout.write("\r" + rem_file + "...%d%%" % percent)
      sys.stdout.flush()

Output: MyFileName...9%

And the cursor shows up at the END of the line. Much better.

标签: python http
9条回答
▲ chillily
2楼-- · 2019-03-07 18:43

I used this code:

url = (<file location>)
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)

file_size_dl = 0
block_sz = 8192
while True:
    buffer = u.read(block_sz)
    if not buffer:
        break

    file_size_dl += len(buffer)
    f.write(buffer)
    status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
    status = status + chr(8)*(len(status)+1)
    print status,

f.close()
查看更多
相关推荐>>
4楼-- · 2019-03-07 18:45

If you use the curses package, you have much greater control of the console. It also comes at a higher cost in code complexity and is probably unnecessary unless you are developing a large console-based app.

For a simple solution, you can always put the spinning wheel at the end of the status messge (the sequence of characters |, \, -, / which actually looks nice under blinking cursor.

查看更多
霸刀☆藐视天下
5楼-- · 2019-03-07 18:50

For small files you may need to had this lines in order to avoid crazy percentages:

sys.stdout.write("\r%2d%%" % percent)

sys.stdout.flush()

Cheers

查看更多
劳资没心,怎么记你
6楼-- · 2019-03-07 18:52

For what it's worth, here's the code I used to get it working:

from urllib import urlretrieve
from progressbar import ProgressBar, Percentage, Bar

url = "http://......."
fileName = "file"
pbar = ProgressBar(widgets=[Percentage(), Bar()])
urlretrieve(url, fileName, reporthook=dlProgress)

def dlProgress(count, blockSize, totalSize):
    pbar.update( int(count * blockSize * 100 / totalSize) )
查看更多
我只想做你的唯一
7楼-- · 2019-03-07 18:52

Late to the party, as usual. Here's an implementation that supports reporting progress, like the core urlretrieve:

import urllib2

def urlretrieve(urllib2_request, filepath, reporthook=None, chunk_size=4096):
    req = urllib2.urlopen(urllib2_request)

    if reporthook:
        # ensure progress method is callable
        if hasattr(reporthook, '__call__'):
            reporthook = None

        try:
            # get response length
            total_size = req.info().getheaders('Content-Length')[0]
        except KeyError:
            reporthook = None

    data = ''
    num_blocks = 0

    with open(filepath, 'w') as f:
        while True:
            data = req.read(chunk_size)
            num_blocks += 1
            if reporthook:
                # report progress
                reporthook(num_blocks, chunk_size, total_size)
            if not data:
                break
            f.write(data)

    # return downloaded length
    return len(data)
查看更多
登录 后发表回答