有某种方法可以跟踪 Shutil.make_archive() 的进度吗?

The*_*gri 8 shutil python-3.x

我正在制作一个通过 压缩文件夹的脚本shutil.make_archive,但文件夹可能很大,所以需要一段时间。有什么方法可以显示完成的百分比吗?到目前为止,我一直在检查该threading模块,但还没有发现太多

这是我想要的输出的示例:

ubuntu@ubuntu:~$./script foldername
Compressing foldername.zip (15%)
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码:

ubuntu@ubuntu:~$./script foldername
Compressing foldername.zip (15%)
Run Code Online (Sandbox Code Playgroud)

您可以进行任何必要的更改以使其正常工作

小智 0

我认为没有任何方法可以跟踪进度,shutil.make_archive()但通过结合 tqdm 和 zipfile,可以达到预期的结果。这是执行此操作的代码片段:

from tqdm import tqdm
import zipfile

zip_file_path = "/tmp/tmp.zip"
local_path = # Whatever file or directory you wand to compress

with zipfile.ZipFile(zip_file_path, mode="w") as archive:
    for file_path in tqdm(list(Path(local_path).rglob("*")), desc='Adding to Zip '):
        archive.write(
            file_path,
            arcname=file_path.relative_to(local_path)
        )

Run Code Online (Sandbox Code Playgroud)

最终结果如下所示: 在此输入图像描述