import os | |
import zipfile | |
def zip_subdirectories(parent_directory, target_directory): | |
if not os.path.exists(target_directory): | |
os.mkdir(target_directory) | |
for foldername, subfolders, filenames in os.walk(parent_directory): | |
# 对每个直接子目录进行操作 | |
for subfolder in subfolders: | |
zip_path = os.path.join(target_directory, f"{subfolder}.zip") | |
# 创建Zip文件 | |
with zipfile.ZipFile(zip_path, 'w') as zipf: | |
subfolder_path = os.path.join(foldername, subfolder) | |
# 遍历子目录下的所有文件和子目录 | |
for root, _, files in os.walk(subfolder_path): | |
for file in files: | |
# 获取文件的绝对路径 | |
file_path = os.path.join(root, file) | |
# 计算文件在zip文件中的路径 | |
arcname = os.path.join(subfolder, os.path.relpath(file_path, start=subfolder_path)) | |
# 将文件添加到zip文件中 | |
zipf.write(file_path, arcname) | |
# 由于os.walk()也会递归地访问子目录,我们可以跳过后续的遍历 | |
break | |
# 调用函数 | |
zip_subdirectories('/mnt/petrelfs/zhuchenglin/LLaVA/playground/data/eval/gqa/data', '/mnt/hwfile/zhuchenglin/playground/eval/gqa/data') | |