提交 7593f3ab 编写于 作者: T Tao Luo 提交者: GitHub

Merge pull request #1538 from helinwang/print

dataset: print download progress
...@@ -16,6 +16,7 @@ import requests ...@@ -16,6 +16,7 @@ import requests
import hashlib import hashlib
import os import os
import shutil import shutil
import sys
__all__ = ['DATA_HOME', 'download', 'md5file'] __all__ = ['DATA_HOME', 'download', 'md5file']
...@@ -41,9 +42,24 @@ def download(url, module_name, md5sum): ...@@ -41,9 +42,24 @@ def download(url, module_name, md5sum):
filename = os.path.join(dirname, url.split('/')[-1]) filename = os.path.join(dirname, url.split('/')[-1])
if not (os.path.exists(filename) and md5file(filename) == md5sum): if not (os.path.exists(filename) and md5file(filename) == md5sum):
print "Cache file %s not found, downloading %s" % (filename, url)
r = requests.get(url, stream=True) r = requests.get(url, stream=True)
with open(filename, 'w') as f: total_length = r.headers.get('content-length')
shutil.copyfileobj(r.raw, f)
if total_length is None:
with open(filename, 'w') as f:
shutil.copyfileobj(r.raw, f)
else:
with open(filename, 'w') as f:
dl = 0
total_length = int(total_length)
for data in r.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done,
' ' * (50 - done)))
sys.stdout.flush()
return filename return filename
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册