Skip to content

Instantly share code, notes, and snippets.

@pmeier
Created June 9, 2022 19:05
Show Gist options
  • Save pmeier/fff0e35f70a1c9f12ec9c1a3c31045b6 to your computer and use it in GitHub Desktop.
Save pmeier/fff0e35f70a1c9f12ec9c1a3c31045b6 to your computer and use it in GitHub Desktop.
Simple download function in Python using requests and tqdm
import pathlib
from urllib.parse import urlparse
import requests
import tqdm
def download(url, root=".", *, name=None, chunk_size=32 * 1024):
root = pathlib.Path(root)
root.mkdir(exist_ok=True, parents=True)
if not name:
name = pathlib.Path(urlparse(url).path).name
file = root / name
with requests.get(url, stream=True) as response, open(file, "wb") as f:
with tqdm.tqdm(
total=int(response.headers["Content-Length"]),
unit="B",
unit_scale=True,
unit_divisor=1024,
dynamic_ncols=True,
) as progress_bar:
for chunk in response.iter_content(chunk_size):
f.write(chunk)
progress_bar.update(len(chunk))
return file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment