import os import shutil from pathlib import Path def copy_with_filtered_figures(source_root, target_root): """ Copy the directory structure from source_root to target_root, excluding all folders that start with 'figures' directly under each dataset folder. Args: source_root: Path to the source directory (/Users/catb/Downloads/result_0.04_r) target_root: Path to the target directory (/Users/catb/Downloads/result_0.04_r_filtered) """ # Create target root directory if it doesn't exist if not os.path.exists(target_root): os.makedirs(target_root) # First level: Get all dataset directories for dataset_name in os.listdir(source_root): dataset_path = os.path.join(source_root, dataset_name) # Skip if not a directory if not os.path.isdir(dataset_path): continue # Create corresponding dataset directory in target target_dataset_path = os.path.join(target_root, dataset_name) if not os.path.exists(target_dataset_path): os.makedirs(target_dataset_path) # Process contents of each dataset directory for item in os.listdir(dataset_path): item_path = os.path.join(dataset_path, item) target_item_path = os.path.join(target_dataset_path, item) # Skip folders that start with 'figures' if os.path.isdir(item_path) and item.startswith('figures'): continue # Copy directories (recursively) or files if os.path.isdir(item_path): shutil.copytree(item_path, target_item_path) else: shutil.copy2(item_path, target_item_path) # Define source and target directories with absolute paths source_directory = "/Users/catb/Downloads/result_0.04_f" target_directory = source_directory + "_filtered" # Run the filtering and copying process copy_with_filtered_figures(source_directory, target_directory) print(f"完成! 已创建筛选后的目录结构: {target_directory}")