def get_file(fname, origin, outdir):
'''
Parameters
----------
fname: output file name
origin: url, link
outdir: path to output dir
'''
fpath = os.path.join(outdir, fname)
# ====== remove empty folder ====== #
if os.path.exists(fpath):
if os.path.isdir(fpath) and len(os.listdir(fpath)) == 0:
shutil.rmtree(fpath)
# ====== download package ====== #
if not os.path.exists(fpath):
prog = Progbar(target=-1, name="Downloading: %s" % origin,
print_report=True, print_summary=True)
def dl_progress(count, block_size, total_size):
if prog.target < 0:
prog.target = total_size
else:
prog.add(count * block_size - prog.seen_so_far)
error_msg = 'URL fetch failure on {}: {} -- {}'
try:
try:
urlretrieve(origin, fpath, dl_progress)
except URLError as e:
raise Exception(error_msg.format(origin, e.errno, e.reason))
except HTTPError as e:
raise Exception(error_msg.format(origin, e.code, e.msg))
except (Exception, KeyboardInterrupt) as e:
if os.path.exists(fpath):
os.remove(fpath)
raise
return fpath
# ===========================================================================
# Python utilities
# ===========================================================================
评论列表
文章目录