Skip to content

Instantly share code, notes, and snippets.

@raman-r-4978
Last active October 22, 2020 08:42
Show Gist options
  • Save raman-r-4978/a0109676d8d26250f10814bdfc568b07 to your computer and use it in GitHub Desktop.
Save raman-r-4978/a0109676d8d26250f10814bdfc568b07 to your computer and use it in GitHub Desktop.
Download files using python
from typing import IO
import requests
from requests.exceptions import RequestException
from tqdm import tqdm
def download_file(url: str, tmp_file: IO) -> None:
try:
r = requests.get(url)
r.raise_for_status()
content_length = r.headers.get("Content-Length")
total = int(content_length) if content_length is not None else None
with tqdm(total=total, desc="downloading") as pbar:
with tmp_file as fptr:
fptr.write(r.content)
pbar.update(len(r.content))
except RequestException as err:
raise SystemExit(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment