import os import shutil # 目标目录(你想要将所有文件移动到的目录) destination_dir = '/public/home/wangshuo/gap/assembly/data/car_1k/subset/ldr' # 替换为目标路径 # 根目录,包含所有相对路径的文件所在的基础目录 root_dir = '/public/home/wangshuo/gap/assembly/data/car_1k' # 替换为你的根目录路径 # 确保目标目录存在 if not os.path.exists(destination_dir): os.makedirs(destination_dir) # 文件列表 TXT 路径 file_list_path = '/public/home/wangshuo/gap/assembly/data/car_1k/subset/optimal_ldr_subset.txt' # 替换为你的 TXT 文件路径 # 打开并读取文件列表 with open(file_list_path, 'r') as f: for line in f: # 去掉每行的空格和换行符 relative_path = line.strip() # 跳过空行 if not relative_path: continue # 获取绝对路径 file_path = os.path.join(root_dir, relative_path) # 检查源文件是否存在 if os.path.exists(file_path): # 获取目标文件的完整路径(保留文件名) dest_file = os.path.join(destination_dir, os.path.basename(file_path)) # 如果目标文件已经存在,可以选择覆盖或者跳过 if os.path.exists(dest_file): print(f"文件 {file_path} 已经存在于目标目录,跳过复制") continue # 移动文件到目标目录 shutil.copy(file_path, dest_file) print(f"已将 {file_path} 复制到 {dest_file}") else: print(f"源文件 {file_path} 不存在,跳过") print("所有文件已处理完成!")