Created
November 22, 2019 07:13
-
-
Save binhna/ccdc42f22178e22ec2d7bd6cce250848 to your computer and use it in GitHub Desktop.
Download a file with tqdm
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
from tqdm import tqdm | |
import requests | |
url = "http://www.ovh.net/files/10Mb.dat" #big file test | |
file_name = url.split('/')[-1] | |
# 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 #1 Kibibyte | |
t=tqdm(total=total_size, unit='iB', unit_scale=True) | |
with open(file_name, 'wb') as f: | |
for data in r.iter_content(block_size): | |
t.update(len(data)) | |
f.write(data) | |
t.close() | |
if total_size != 0 and t.n != 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