from urllib.request import urlretrieveimport sysimport osprev_reported_download_percent = None# 首先定义下载 hook,作为 urllib.request.urlretrive 的关键字参数def download_hook(count, block_size, total_size): """ 接口是写死的 """ global prev_reported_download_percent percent = int(count*block_size*100/total_size) if prev_reported_download_percent != percent: if percent % 5 == 0: sys.stdout.write('%s%%' % percent) sys.stdout.flush() else: sys.stdout.write('.') sys.stdout.flush() prev_reported_download_percent = percentdef maybe_download(filename, force=False): """ force 表示是否强制下载 """ if force or not os.path.exists(filename): print('Attempting to download') filename, _ = urlretrieve(url+filename, filename, reporthook=download_hook) # url+filename:表示文件的 url 地址, # filename 则为保存到本地时的文件名 print('\nDownload completed!') # statinfo = os.stat(filename) return filename
比如对于 http://ufldl.stanford.edu/housenumbers/
网站下的 train.tar.gz
/test.tar.gz
两文件,客户端的调用方式为:
url = 'http://ufldl.stanford.edu/housenumbers/'train_filename = maybe_download('train.tar.gz')test_filename = maybe_download('test.tar.gz')