V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
CivAx
V2EX  ›  Python

有办法可以把 progressbar2 和 tarfile 结合起来用吗?

  •  
  •   CivAx · 2019-10-25 16:49:32 +08:00 · 1945 次点击
    这是一个创建于 1637 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有一个约 19GB 的 tar.gz 零散文件包需要解压(解压后约 80G ),由于是有人值守,所以迫切需要一个进度条。

    tarfile 代码:

    import tarfile
    
    filepath ="/data/someWhere/2019"
    list = '(tarfile.open("/data/someWhere/2019.gz").getnames())'   # list
    for n in list:
      tar.extract(n. filepath)
    tar.close()
    

    progress2 代码:

    import progress
    
    size=len(list)  # int, e.g.127
    with progressbar.ProgressBar(
      widgets=[progressbar.SimpleProgress()],
      max_value=(size)) as bar:
      
      for i in range(size):
        bar.update(i+1)
      bar.finish()
    

    尝试了把 progress2 的方法插入到 tarfile 的 for 循环当中,由于是用的 extract 而不是 extractall,导致进度条完全没有效果,全部解压完毕后会从 0 直接到 100%(包内有 127 个文件)。

    如果反过来插入,会导致压缩包被重复解压 ${文件数量} 次。

    2 条回复    2019-10-26 10:37:34 +08:00
    by73
        1
    by73  
       2019-10-25 23:05:51 +08:00
    照着你的代码合并了一次,没有任何问题啊 https://pastebin.com/rJRhQcnn
    CivAx
        2
    CivAx  
    OP
       2019-10-26 10:37:34 +08:00
    @by73 #1 您试一下在压缩包中打包多个目录与文件,应该会出现我描述的问题了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3757 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:14 · PVG 08:14 · LAX 17:14 · JFK 20:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.