Created
January 10, 2022 13:51
-
-
Save alphazwest/4f1c50d5cf01cc4114b7f3ff6a2ca58c 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 time | |
import requests | |
# use a context manager to make an HTTP request and file | |
with requests.get("https://www.example.com/file.txt", stream=True) as r: | |
with open('download.txt', 'wb') as file: | |
# Get the total size, in bytes, from the response header | |
total_size = int(r.headers.get('Content-Length')) | |
# Define the size of the chunk to iterate over (Mb) | |
chunk_size = 1 | |
# iterate over every chunk and calculate % of total | |
for i, chunk in enumerate(r.iter_content(chunk_size=chunk_size)): | |
# calculate current percentage | |
c = i * chunk_size / total_size * 100 | |
# write current % to console, pause for .1ms, then flush console | |
sys.stdout.write(f"\r{round(c, 4)}%") | |
time.sleep(.1) | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment