Created
September 25, 2019 12:53
-
-
Save Pragith/770a3924a6b9474243fa73df6a220a53 to your computer and use it in GitHub Desktop.
Download file in Python with progress bar
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
def download_file(url, output_location): | |
print('URL:',url) | |
# Streaming, so we can iterate over the response. | |
r = requests.get(url, stream=True) | |
# Total size in bytes. | |
total_size = int(r.headers.get('content-length', 0)); | |
block_size = 1024 | |
wrote = 0 | |
with open(output_location, 'wb') as f: | |
for data in tqdm(r.iter_content(block_size), total=math.ceil(total_size//block_size) , unit='KB', unit_scale=True): | |
wrote = wrote + len(data) | |
f.write(data) | |
if total_size != 0 and wrote != total_size: | |
print("ERROR, something went wrong") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment