
本文将指导你如何使用 Python 的 zipfile 模块,将目录中的多个文件夹压缩成单独的 zip 文件,并实时显示每个文件压缩完成的进度。通过简单的代码修改,你可以在控制台中看到每个 zip 文件的压缩路径,从而实现交互式的压缩体验。
基础代码
首先,我们回顾一下用于压缩目录中子文件夹的基础代码:
import osimport zipfileINPUT_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 locationif __name__ == '__main__': zip_subfolders(INPUT_FOLDER, OUTPUT_FOLDER)
这段代码定义了两个关键函数:create_zip 用于将单个文件夹压缩成 zip 文件,zip_subfolders 用于遍历输入目录中的所有子文件夹并调用 create_zip。
实现交互式进度显示
为了实现交互式进度显示,我们需要在 create_zip 函数中添加一行代码,用于打印已压缩文件的路径。
立即学习“Python免费学习笔记(深入)”;
import osimport zipfileINPUT_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 statementdef 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 locationif __name__ == '__main__': zip_subfolders(INPUT_FOLDER, OUTPUT_FOLDER)
这行代码 print(f’Zipped: {zipped_filepath}’) 使用 f-string 打印出当前压缩完成的 zip 文件的路径。
运行结果
现在,当你运行修改后的代码时,控制台将会在每个文件夹压缩完成后显示类似如下的信息:
Zipped: zipped/folder1.zipZipped: zipped/folder2.zipZipped: zipped/folder3.zip...
这样,你就可以清楚地看到每个文件的压缩进度。
总结
通过在 create_zip 函数中添加一个简单的 print 语句,我们成功地实现了交互式的压缩进度显示。这个方法非常简单有效,可以帮助你更好地了解 Python 脚本的执行情况。你还可以根据需要,进一步扩展这个功能,例如添加进度条、使用日志记录等。
以上就是Python 交互式压缩:实时跟踪文件压缩进度的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1375263.html
微信扫一扫
支付宝扫一扫