如何打印压缩文件python的百分比 [英] How to print the percentage of zipping a file python

查看:43
本文介绍了如何打印压缩文件python的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在压缩文件时获得文件的百分比.例如,它会打印 1%、2%、3% 等.我不知道从哪里开始.我现在该怎么做,我只有压缩文件的代码.

I would like to get the percentage a file is at while zipping it. For instance it will print 1%, 2%, 3%, etc. I have no idea on where to start. How would I go about doing this right now I just have the code to zip the file.

代码:

zipPath = zipfile.ZipFile("Files/Zip/" + pic + ".zip", "w")

for root, dirs, files in os.walk(filePath):
    for file in files:
        zipPath.write(os.path.join(root, file), str(pic) + "\\" + file)

print("Done")
zipPath.close()

推荐答案

遗憾的是,您无法从 zipfile 模块中获得每个文件的压缩进度,但您可以通过跟踪了解总进度到目前为止您已经处理了多少字节.

Unfortunately, you can't get progress on the compression of each individual file from the zipfile module, but you can get an idea of the total progress by keeping track of how many bytes you've processed so far.

正如 Mikko Ohtamaa 所建议的,最简单的方法是遍历文件列表两次,首先确定文件大小,然后进行压缩.但是,正如 Kevin 提到的,目录的内容可能会在这两次传递之间发生变化,因此数字可能不准确.

As Mikko Ohtamaa suggested, the easiest way to do this is to walk through the file list twice, first to determine the file sizes, and second to do the compression. However, as Kevin mentioned the contents of the directory could change between these two passes, so the numbers may be inaccurate.

下面的程序(为 Python 2.6 编写)说明了这个过程.

The program below (written for Python 2.6) illustrates the process.

#!/usr/bin/env python

''' zip all the files in dirname into archive zipname

    Use only the last path component in dirname as the 
    archive directory name for all files

    Written by PM 2Ring 2015.02.15

    From http://stackoverflow.com/q/28522669/4014959
'''

import sys
import os
import zipfile


def zipdir(zipname, dirname):
    #Get total data size in bytes so we can report on progress
    total = 0
    for root, dirs, files in os.walk(dirname):
        for fname in files:
            path = os.path.join(root, fname)
            total += os.path.getsize(path)

    #Get the archive directory name
    basename = os.path.basename(dirname)

    z = zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED)

    #Current data byte count
    current = 0
    for root, dirs, files in os.walk(dirname):
        for fname in files:
            path = os.path.join(root, fname)
            arcname = os.path.join(basename, fname)
            percent = 100 * current / total
            print '%3d%% %s' % (percent, path)

            z.write(path, arcname)
            current += os.path.getsize(path)
    z.close()


def main():
    if len(sys.argv) < 3:
        print 'Usage: %s zipname dirname' % sys.argv[0]
        exit(1)

    zipname = sys.argv[1]
    dirname = sys.argv[2]
    zipdir(zipname, dirname)


if __name__ == '__main__':
    main()

请注意,我使用 zipfile.ZIP_DEFLATED 压缩参数打开了 zip 文件;默认为 zipfile.ZIP_STORED,即不执行压缩.此外,zip 文件可以处理 DOS 样式和 Unix 样式的路径分隔符,因此您不需要在存档路径名中使用反斜杠,并且正如我的代码所示,您可以使用 os.path.join() 构造存档路径名.

Note that I open the zip file with the zipfile.ZIP_DEFLATED compression argument; the default is zipfile.ZIP_STORED, i.e., no compression is performed. Also, zip files can cope with both DOS-style and Unix-style path separators, so you don't need to use backslashes in your archive pathnames, and as my code shows you can just use os.path.join() to construct the archive pathname.

顺便说一句,在您的代码中,您的内部 for 循环中有 str(pic) .通常,在循环内重新评估具有常量参数的函数有点浪费.但在这种情况下,这完全是多余的,因为从您的第一条语句看来 pic 已经是一个字符串.

BTW, in your code you have str(pic) inside your inner for loop. In general, it's a bit wasteful re-evaluating a function with a constant argument inside a loop. But in this case, it's totally superfluous, since from your first statement it appears that pic is already a string.

这篇关于如何打印压缩文件python的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆