0

This is the best one so far

I have seen many questions on stackoverflow but none of the answers give a simple elegant method.

link = "http://download.thinkbroadband.com/10MB.zip"
file_name = "test"
with open(file_name, "wb") as f:
        print('Downloading: {}'.format(file_name))
        response = requests.get(link, stream=True)
        total_length = response.headers.get('content-length')

        if total_length is None:
            f.write(response.content)
        else:
            dl = 0
            total_length = int(total_length)
            for data in response.iter_content(chunk_size=4096):
                dl += len(data)
                f.write(data)
                done = int(50 * dl / total_length)
                sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)))
                sys.stdout.flush()

Can I get something with more details when a file is downloaded? All the previous questions do not have a good simple answer.

Varun
  • 91
  • 1
  • 2
  • 17
  • Unless you have no argument WHY you don't want to use this it's hard to suggest something different (which you probably also don't want to use then) – DonGru Sep 01 '17 at 06:41
  • Okay my re-phrased question : I wanted more details when I download from a link. – Varun Sep 01 '17 at 06:43

1 Answers1

2

Why reinvent the wheel? Use tqdm. Follow the link and follow the instructions to import tqdm and add a progress bar for any iteration. For example:

from tqdm import tqdm
...
for data in tqdm(response.iter_content(chunk_size=4096)):
    # additional logic here
...

Read the examples in the provided pypi link to add additional information to your progress bar.

kekkler
  • 326
  • 2
  • 4