
本文将指导你如何使用 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免费学习笔记(深入)”;
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号