File size: 9,694 Bytes
1a19f57 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | # import os
# import shutil
# # --- 1. 配置您的文件夹路径 ---
# # 源文件夹列表 (您的 x_part0, x_part1, ...)
# SOURCE_FOLDERS = ['/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug_part0', '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug_part1', '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug_part2']
# # 您希望创建的新文件夹的名称
# TARGET_FOLDER = 'samples_vbench_all_dimension_aug'
# # --- 脚本正文 ---
# def merge_folders(source_dirs, target_dir):
# """
# 合并所有源文件夹到目标文件夹,保持子目录结构。
# """
# print(f"开始合并文件夹到 '{target_dir}'...")
# total_files_copied = 0
# total_files_skipped = 0
# # 1. 确保目标文件夹存在
# # os.makedirs(..., exist_ok=True) 会创建文件夹,如果它已经存在也不会报错
# os.makedirs(target_dir, exist_ok=True)
# # 2. 遍历每一个源文件夹 (x_part0, x_part1, ...)
# for source_dir in source_dirs:
# print(f"\n--- 正在处理源文件夹: '{source_dir}' ---")
# # 3. 遍历源文件夹中的所有文件和子文件夹
# # os.walk 会递归地遍历所有子目录
# # dirpath: 当前子目录的路径 (例如: 'x_part0/train/classA')
# # dirnames: 当前子目录下的文件夹列表
# # filenames: 当前子目录下的文件列表
# for dirpath, dirnames, filenames in os.walk(source_dir):
# # 4. 计算当前文件在目标文件夹中应有的路径
# # os.path.relpath 会计算相对路径
# # 例如: dirpath = 'x_part0/train/classA'
# # source_dir = 'x_part0'
# # relative_path = 'train/classA'
# relative_path = os.path.relpath(dirpath, source_dir)
# # 目标子目录的完整路径
# # 例如: 'x_merged/train/classA'
# target_subdir = os.path.join(target_dir, relative_path)
# # 5. 如果目标子目录不存在,则创建它
# if not os.path.exists(target_subdir):
# os.makedirs(target_subdir)
# # 6. 遍历当前子目录下的所有文件并复制
# for filename in filenames:
# source_file = os.path.join(dirpath, filename)
# target_file = os.path.join(target_subdir, filename)
# # 7. 处理文件名冲突
# if os.path.exists(target_file):
# print(f"警告: 文件 '{target_file}' 已存在,跳过。")
# total_files_skipped += 1
# else:
# try:
# # 使用 shutil.copy2 来复制文件
# # copy2 会同时复制文件内容和元数据(如修改时间)
# shutil.copy2(source_file, target_file)
# total_files_copied += 1
# except Exception as e:
# print(f"错误: 复制文件 {source_file} 时失败: {e}")
# total_files_skipped += 1
# print("\n--- 合并完成 ---")
# print(f"总共成功复制: {total_files_copied} 个文件")
# print(f"总共跳过(因已存在或错误): {total_files_skipped} 个文件")
# # --- 运行主函数 ---
# if __name__ == "__main__":
# # 检查源文件夹是否存在
# valid_source_folders = []
# for folder in SOURCE_FOLDERS:
# if os.path.isdir(folder):
# valid_source_folders.append(folder)
# else:
# print(f"警告: 找不到源文件夹 '{folder}',将跳过。")
# if not valid_source_folders:
# print("错误: 找不到任何有效的源文件夹。脚本终止。")
# else:
# merge_folders(valid_source_folders, TARGET_FOLDER)
import os
import shutil
# --- 1. 请修改这里的配置 ---
# 您要从中复制文件的五个源文件夹
# SOURCE_FOLDERS = [
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30/ckpts/steps_ckpt_Oct14_6_3000',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed0/ckpts/steps_ckpt_Oct14_6_3000',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed84/ckpts/steps_ckpt_Oct14_6_3000',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed128/ckpts/steps_ckpt_Oct14_6_3000',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed1000/ckpts/steps_ckpt_Oct14_6_3000',
# ]
# SOURCE_FOLDERS = [
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed0/output_pretrain',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed42/output_pretrain',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed84/output_pretrain',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed128/output_pretrain',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30-seed1000/output_pretrain',
# ]
seeds = ['r1', 'r2', 'r3', 'r4']
model_path = 'output_Oct16_1/steps_2000'
model_name = 'oct16_1'
folder_sub = 'all_dimension_aug'
# folder_sub = 'all_category'
# SOURCE_FOLDERS = [
# f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/{i}/wan-videos-t2v-512*288-30-seed-1/output_Oct14_6/steps_2000' for i in seeds
# ]
# SOURCE_FOLDERS.append('/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan-videos-t2v-512*288-30/output_Oct14_6/steps_2000')
SOURCE_FOLDERS = [
f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_{folder_sub}/{i}/wan-videos-t2v-512*288-30-seed-1/{model_path}' for i in seeds
]
SOURCE_FOLDERS.append(f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_{folder_sub}/wan-videos-t2v-512*288-30/{model_path}')
# SOURCE_FOLDERS = [
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_category/wan_rwft',
# '/nfs/ywang29/Reward_finetuning/VideoX-Fun/validation_samples/samples_vbench_all_dimension_aug/wan_rwft'
# ]
# 您希望将所有文件汇集到的目标文件夹
TARGET_FOLDER = f'/nfs/ywang29/Reward_finetuning/VideoX-Fun/VBench/sampled_videos/wan1.3b_rwft_{model_name}_2000'
# --- 脚本正文 (通常不需要修改) ---
def copy_all_files(source_dirs, target_dir):
"""
将所有源文件夹中的全部文件递归复制到目标文件夹的根目录下。
"""
print(f"开始复制文件到: {target_dir}")
# 1. 确保目标文件夹存在
os.makedirs(target_dir, exist_ok=True)
# 2. 初始化计数器
total_copied = 0
total_skipped = 0
total_errors = 0
# 3. 遍历每一个源文件夹
for source_dir in source_dirs:
if not os.path.isdir(source_dir):
print(f"\n警告: 源文件夹 '{source_dir}' 不存在,已跳过。")
continue
print(f"\n--- 正在处理文件夹: {source_dir} ---")
# 4. 递归遍历 (os.walk) 源文件夹中的所有内容
# dirpath: 当前子目录的路径
# dirnames: 当前子目录下的文件夹列表
# filenames: 当前子目录下的文件列表
for dirpath, dirnames, filenames in os.walk(source_dir):
# 5. 遍历当前子目录下的所有文件
for filename in filenames:
# 构建完整的源文件路径
source_file_path = os.path.join(dirpath, filename)
# 构建完整的目标文件路径(直接放在目标文件夹的根下)
target_file_path = os.path.join(target_dir, filename)
# 6. 检查文件名冲突:如果目标文件已存在
if os.path.exists(target_file_path):
print(f" [跳过] 文件 '{filename}' 已存在于目标文件夹中。")
total_skipped += 1
else:
# 7. 执行复制
try:
# shutil.copy2 会复制文件内容和元数据(如时间戳)
shutil.copy2(source_file_path, target_file_path)
print(f" [复制] {filename}")
total_copied += 1
except Exception as e:
print(f" [失败] 复制 '{source_file_path}' 时出错: {e}")
total_errors += 1
print("\n--- 操作完成 ---")
print(f"总共成功复制: {total_copied} 个文件")
print(f"总共跳过(因文件名冲突): {total_skipped} 个文件")
print(f"总共失败(因错误): {total_errors} 个文件")
# --- 运行主函数 ---
if __name__ == "__main__":
copy_all_files(SOURCE_FOLDERS, TARGET_FOLDER) |