__init__.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:odin 作者: imito 项目源码 文件源码
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
# ===========================================================================
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号