Created
January 10, 2022 03:04
-
-
Save alphazwest/182a9baa3a85953bcfc7b17f72251fbe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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