'How can I get size of file while downloading it in Python

How can I get size of file while downloading it in Python so that I can show downloaded size and pending size of file to download? if file size is 2GB and 700mb is downloaded then how will we get that in python. Currently using Python requests module to download file.

example code:

import requests
r = requests.get(download_link)
with open('setup.exe','wb') as file:
    file.write()


Solution 1:[1]

You can get the file size from the header

import requests
r = requests.get(download_link)
headers = r.headers
file_size = headers.get('Content-Length')

Solution 2:[2]

I found what i was looking for: ref: https://requests.readthedocs.io/en/master/user/advanced/#body-content-workflow

with requests.get(url, stream=True) as r:
   downloaded=0
   total =  int(r.headers['content-length'])
   for i in r.iter_content():
       downloaded+=len(i)
       print(downloaded)

Solution 3:[3]

I too was looking for a solution and found one. Use requests with stream=True and tqdm.

To install tqdm with python3, use:

python3 -m pip install tqdm

Code to download a video from MITOpenCourseWare:

import requests
from tqdm import tqdm

link = 'http://www.archive.org/download/MIT6.006F11/MIT6_006F11_lec01_300k.mp4'
filename = 'video.mp4'
response = requests.get(link, stream=True)
if response.status_code == requests.codes.ok:
    total_size = int(response.headers.get('Content-Length', 0))
    downloaded_size = 0
    print(f'contentLength: {total_size} MB')
    with open(filename, 'wb') as handle,\
        tqdm(unit='B', total=total_size, desc=filename, unit_scale=True, unit_divisor=1024) as bar:
        for chunk in response.iter_content(chunk_size=1024*1024):
            size = handle.write(chunk)
            bar.update(size)
            downloaded_size += size

The variable downloaded_size for each loop gives the current downloaded size and total_size gives the total size.

Assumptions:

  1. The website responds with a Content-Length

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tuchunxiao
Solution 2 Deepak
Solution 3 Allan