Last active
October 22, 2020 08:42
-
-
Save raman-r-4978/a0109676d8d26250f10814bdfc568b07 to your computer and use it in GitHub Desktop.
Download files using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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