语言:python 3
用法:选择目录,对该目录下的文件夹分别压缩,生成同名压缩文件,并保存到该目录下。
import os
import shutil
import zipfile
from tkinter import Tk
from tkinter import filedialog
root = Tk()
root.withdraw()
directory = filedialog.askdirectory(title="选择目录")
def zip_folders(directory):
for root, dirs, files in os.walk(directory):
for dir_name in dirs:
folder_path = os.path.join(root, dir_name)
zip_path = f"{folder_path}"
shutil.make_archive(zip_path, 'zip', folder_path)
print(f"已压缩文件夹: {zip_path}")
zip_folders(directory)
zip_folders(directory)