Deepak
Deepak

Reputation: 59

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()

Upvotes: 3

Views: 1932

Answers (3)

Allan
Allan

Reputation: 21

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

Upvotes: 2

Deepak
Deepak

Reputation: 59

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)

Upvotes: 0

tuchunxiao
tuchunxiao

Reputation: 171

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')

Upvotes: 1

Related Questions