import os import shutil def sync_files_and_folders(path_a, path_b, exts): def should_copy(file): return any(file.lower().endswith(ext) for ext in exts) def sync_one_way(src_root, dst_root): for root, dirs, files in os.walk(src_root): rel_path = os.path.relpath(root, src_root) target_root = os.path.join(dst_root, rel_path) # 确保目标目录存在 if not os.path.exists(target_root): print(f"Creating folder: {target_root}") os.makedirs(target_root) for file in files: if should_copy(file): src_file = os.path.join(root, file) tgt_file = os.path.join(target_root, file) if not os.path.exists(tgt_file): print(f"Copying {src_file} -> {tgt_file}") shutil.copy2(src_file, tgt_file) # 正向同步:A → B sync_one_way(path_a, path_b) # 反向同步:B → A sync_one_way(path_b, path_a) # 用法示例 path_a = "/Users/oushiha/Desktop/swangnice_site/content/en" path_b = "/Users/oushiha/Desktop/swangnice_site/content/zh" sync_extensions = ['.md', '.png', '.jpg', '.jpeg', '.gif', '.webp'] sync_files_and_folders(path_a, path_b, sync_extensions)