那就用代码实现把
import os
from zipfile import ZipFile
def zip_files_with_same_name(folder_path, output_folder):
os.makedirs(output_folder, exist_ok=True)
# 遍历文件夹及其子文件夹
for root, _, files in os.walk(folder_path):
# 创建一个字典,以文件名(不包含后缀)为键,文件路径列表为值
file_dict = {}
for file in files:
file_name, file_ext = os.path.splitext(file)
file_key = file_name.lower() # 忽略文件名的大小写
file_path = os.path.join(root, file)
if file_key in file_dict:
file_dict[file_key].append(file_path)
else:
file_dict[file_key] = [file_path]
# 遍历字典,对每组同名文件进行压缩
for key, file_paths in file_dict.items():
if len(file_paths) > 1:
rel_path = os.path.relpath(file_paths[0], folder_path)
output_subfolder = os.path.join(output_folder, os.path.dirname(rel_path))
os.makedirs(output_subfolder, exist_ok=True)
zip_file_path = os.path.join(output_subfolder, f"{key}.zip")
with ZipFile(zip_file_path, 'w') as zipf:
for file_path in file_paths:
rel_file_path = os.path.relpath(file_path, folder_path)
zipf.write(file_path, rel_file_path)
if __name__ == "__main__":
current_folder = os.getcwd()
output_folder = os.path.join(current_folder, "output")
# 执行压缩
zip_files_with_same_name(current_folder, output_folder)
这个代码可以将目录下所有文件夹下的同名文件压缩在一起,并保留文件路径还有两个分别是只压缩当前文件夹下的文件、压缩所有文件夹下的文件,不保留文件路径
批量压缩同名文件(目录下文件).py
import os
from zipfile import ZipFile
def zip_files_with_same_name(folder_path, output_folder):
# 获取文件夹下的所有文件
files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
# 创建一个字典,以文件名(不包含后缀)为键,文件路径列表为值
file_dict = {}
for file in files:
file_name, file_ext = os.path.splitext(file)
file_key = file_name.lower() # 忽略文件名的大小写
file_path = os.path.join(folder_path, file)
if file_key in file_dict:
file_dict[file_key].append(file_path)
else:
file_dict[file_key] = [file_path]
# 创建输出文件夹(如果不存在)
os.makedirs(output_folder, exist_ok=True)
# 遍历字典,对每组同名文件进行压缩
for key, file_paths in file_dict.items():
if len(file_paths) > 1:
zip_file_path = os.path.join(output_folder, f"{key}.zip")
with ZipFile(zip_file_path, 'w') as zipf:
for file_path in file_paths:
zipf.write(file_path, os.path.basename(file_path))
if __name__ == "__main__":
# 获取当前工作目录
current_folder = os.getcwd()
# 输出文件夹直接设置为当前工作目录下的 "output" 文件夹
output_folder = os.path.join(current_folder, "output")
# 执行压缩
zip_files_with_same_name(current_folder, output_folder)
批量压缩同名文件5 (所有文件夹,压缩包里面不保留文件路径).py
import os
from zipfile import ZipFile
def zip_files_with_same_name(folder_path, output_folder):
os.makedirs(output_folder, exist_ok=True)
for root, _, files in os.walk(folder_path):
file_dict = {}
for file in files:
file_name, file_ext = os.path.splitext(file)
file_key = file_name.lower()
file_path = os.path.join(root, file)
if file_key in file_dict:
file_dict[file_key].append(file_path)
else:
file_dict[file_key] = [file_path]
for key, file_paths in file_dict.items():
if len(file_paths) > 1:
rel_path = os.path.relpath(file_paths[0], folder_path)
output_subfolder = os.path.join(output_folder, os.path.dirname(rel_path))
os.makedirs(output_subfolder, exist_ok=True)
zip_file_path = os.path.join(output_subfolder, f"{key}.zip")
with ZipFile(zip_file_path, 'w') as zipf:
for file_path in file_paths:
# Write each file without including the file path
zipf.write(file_path, os.path.basename(file_path))
if __name__ == "__main__":
current_folder = os.getcwd()
output_folder = os.path.join(current_folder, "output")
zip_files_with_same_name(current_folder, output_folder)