Files
swangnice_site/scripts/sync_en_zh.py
2025-07-25 02:15:37 +08:00

36 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)