Created
January 2, 2018 15:52
-
-
Save salishdev/a3f66c36ac5fc1fe5ff750dd9538afa4 to your computer and use it in GitHub Desktop.
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
from urllib.request import urlretrieve | |
from os.path import isfile, isdir | |
from tqdm import tqdm | |
import sys | |
class DLProgress(tqdm): | |
last_block = 0 | |
def hook(self, block_num=1, block_size=1, total_size=None): | |
self.total = total_size | |
self.update((block_num - self.last_block) * block_size) | |
self.last_block = block_num | |
def download_with_progress(url, filename=None): | |
if filename is None: | |
filename = url.split('/')[-1] | |
if not isfile(filename): | |
with DLProgress(unit='B', unit_scale=True, miniters=1, desc='progress') as pbar: | |
urlretrieve(url, filename, pbar.hook) | |
else: | |
print("file already exists!") | |
if __name__ == '__main__': | |
download_with_progress(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment