Skip to content

Instantly share code, notes, and snippets.

@alphazwest
Created January 10, 2022 03:04
Show Gist options
  • Save alphazwest/182a9baa3a85953bcfc7b17f72251fbe to your computer and use it in GitHub Desktop.
Save alphazwest/182a9baa3a85953bcfc7b17f72251fbe to your computer and use it in GitHub Desktop.
import requests
import shutil
from tqdm.auto import tqdm
# make an HTTP request within a context manager
with requests.get("https://www.example.com/file.txt", stream=True) as r:
# check header to get content length, in bytes
total_length = int(r.headers.get("Content-Length"))
# implement progress bar via tqdm
with tqdm.wrapattr(r.raw, "read", total=total_length, desc="")as raw:
# save the output to a file
with open(f"{os.path.basename(r.url)}", 'wb')as output:
shutil.copyfileobj(raw, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment