0

Python's urllib.request.urlretrieve() accepts a function that will be called when each chunk is fetched from the network.

The function is called with three arguments: a progressive identifier of the chunk, its size and the total size of the download.

Given those three information can I compute the number of bytes already fetched? This will be used to compute the progress of the download.

I'm tempted to do chunk_number * chunk_size / download_size, but I'm not sure the chunk size is constant for all the chunks.

Giuseppe Crinò
  • 486
  • 3
  • 15
  • 1
    There's unfortunately no reason to assume chunks will all be the same size! If you want to give an accurate download percentage you may have to keep a running sum (e.g. `total_bytes += most_recent_chunk.size`) – Gershom Maes Jul 18 '17 at 17:07

1 Answers1

1

You can keep a running total of all the sizes you've seen so far.

Try this:

import urllib.request


def my_downloader(url, filename = None):
    running_total = 0
    def my_reporthook(count, size, total):
        nonlocal running_total
        running_total += size
        print ("{}%".format(100*running_total//total))
    return urllib.request.urlretrieve(url, filename, my_reporthook)

print (my_downloader('https://www.gutenberg.org/files/55146/55146-h.zip'))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308