Skip to content

Instantly share code, notes, and snippets.

@peter279k
Forked from yanqd0/dl_requests_tqdm.py
Created December 16, 2022 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter279k/45e8ac5743cc0569fc526935c6a9ed96 to your computer and use it in GitHub Desktop.
Save peter279k/45e8ac5743cc0569fc526935c6a9ed96 to your computer and use it in GitHub Desktop.
Python requests download file with a tqdm progress bar
import requests
from tqdm import tqdm
def download(url: str, fname: str, chunk_size=1024):
resp = requests.get(url, stream=True)
total = int(resp.headers.get('content-length', 0))
with open(fname, 'wb') as file, tqdm(
desc=fname,
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in resp.iter_content(chunk_size=chunk_size):
size = file.write(data)
bar.update(size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment