Python 交互式压缩:实时跟踪文件压缩进度

聖光之護
发布: 2025-10-03 11:35:15
原创
703人浏览过

python 交互式压缩:实时跟踪文件压缩进度

本文将指导你如何使用 Python 的 zipfile 模块,将目录中的多个文件夹压缩成单独的 zip 文件,并实时显示每个文件压缩完成的进度。通过简单的代码修改,你可以在控制台中看到每个 zip 文件的压缩路径,从而实现交互式的压缩体验。

基础代码

首先,我们回顾一下用于压缩目录中子文件夹的基础代码:

import os
import zipfile

INPUT_FOLDER = 'to_zip'
OUTPUT_FOLDER = 'zipped'

def create_zip(folder_path, zipped_filepath):
    zip_obj = zipfile.ZipFile(zipped_filepath, 'w')  # create a zip file in the required path
    for filename in next(os.walk(folder_path))[2]: # loop over all the file in this folder
        zip_obj.write(
            os.path.join(folder_path, filename),  # get the full path of the current file
            filename,  # file path in the archive: we put all in the root of the archive
            compress_type=zipfile.ZIP_DEFLATED
        )
    zip_obj.close()

def zip_subfolders(input_folder, output_folder):
    os.makedirs(output_folder, exist_ok=True)  # create output folder if it does not exist
    for folder_name in next(os.walk(input_folder))[1]:  # loop over all the folders in your input folder
        zipped_filepath = os.path.join(output_folder, f'{folder_name}.zip')  # create the path for the output zip file for this folder
        curr_folder_path = os.path.join(input_folder, folder_name)  # get the full path of the current folder
        create_zip(curr_folder_path, zipped_filepath)  # create the zip file and put in the right location

if __name__ == '__main__':
    zip_subfolders(INPUT_FOLDER, OUTPUT_FOLDER)
登录后复制

这段代码定义了两个关键函数:create_zip 用于将单个文件夹压缩成 zip 文件,zip_subfolders 用于遍历输入目录中的所有子文件夹并调用 create_zip。

实现交互式进度显示

为了实现交互式进度显示,我们需要在 create_zip 函数中添加一行代码,用于打印已压缩文件的路径。

立即学习Python免费学习笔记(深入)”;

ViiTor实时翻译
ViiTor实时翻译

AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

ViiTor实时翻译 116
查看详情 ViiTor实时翻译
import os
import zipfile

INPUT_FOLDER = 'to_zip'
OUTPUT_FOLDER = 'zipped'

def create_zip(folder_path, zipped_filepath):
    zip_obj = zipfile.ZipFile(zipped_filepath, 'w')  # create a zip file in the required path
    for filename in next(os.walk(folder_path))[2]: # loop over all the file in this folder
        zip_obj.write(
            os.path.join(folder_path, filename),  # get the full path of the current file
            filename,  # file path in the archive: we put all in the root of the archive
            compress_type=zipfile.ZIP_DEFLATED
        )
    zip_obj.close()
    print(f'Zipped: {zipped_filepath}') # Added print statement

def zip_subfolders(input_folder, output_folder):
    os.makedirs(output_folder, exist_ok=True)  # create output folder if it does not exist
    for folder_name in next(os.walk(input_folder))[1]:  # loop over all the folders in your input folder
        zipped_filepath = os.path.join(output_folder, f'{folder_name}.zip')  # create the path for the output zip file for this folder
        curr_folder_path = os.path.join(input_folder, folder_name)  # get the full path of the current folder
        create_zip(curr_folder_path, zipped_filepath)  # create the zip file and put in the right location

if __name__ == '__main__':
    zip_subfolders(INPUT_FOLDER, OUTPUT_FOLDER)
登录后复制

这行代码 print(f'Zipped: {zipped_filepath}') 使用 f-string 打印出当前压缩完成的 zip 文件的路径。

运行结果

现在,当你运行修改后的代码时,控制台将会在每个文件夹压缩完成后显示类似如下的信息:

Zipped: zipped/folder1.zip
Zipped: zipped/folder2.zip
Zipped: zipped/folder3.zip
...
登录后复制

这样,你就可以清楚地看到每个文件的压缩进度。

总结

通过在 create_zip 函数中添加一个简单的 print 语句,我们成功地实现了交互式的压缩进度显示。这个方法非常简单有效,可以帮助你更好地了解 Python 脚本的执行情况。你还可以根据需要,进一步扩展这个功能,例如添加进度条、使用日志记录等。

以上就是Python 交互式压缩:实时跟踪文件压缩进度的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号