폴더 업데이트
스크립트
import os
import filecmp
import shutil
def sync_directories(source_dir, target_dir):
"""
Sync the target directory to match the source directory.
"""
# Create target directory if it doesn't exist
if not os.path.exists(target_dir):
os.makedirs(target_dir)
# Compare the two directories
comparison = filecmp.dircmp(source_dir, target_dir)
# Update files that are new or have changed
for file_name in comparison.left_only + comparison.diff_files:
source_file = os.path.join(source_dir, file_name)
target_file = os.path.join(target_dir, file_name)
if os.path.isdir(source_file):
if os.path.exists(target_file):
shutil.rmtree(target_file)
shutil.copytree(source_file, target_file)
else:
shutil.copy2(source_file, target_file)
# Delete files that are no longer present in the source directory
for file_name in comparison.right_only:
target_file = os.path.join(target_dir, file_name)
if os.path.isdir(target_file):
shutil.rmtree(target_file)
else:
os.remove(target_file)
# Recursively sync subdirectories
for subdir in comparison.common_dirs:
sync_directories(os.path.join(source_dir, subdir), os.path.join(target_dir, subdir))
def main():
source_content_dir = '../docflow'
target_content_dir = 'content/docs'
source_static_dir = '../docflow/resources'
target_static_dir = 'static/resources'
sync_directories(source_content_dir, target_content_dir)
sync_directories(source_static_dir, target_static_dir)
if __name__ == "__main__":
main()스크립트 설명
sync_directories
source_dir과target_dir을 비교하고 업데이트합니다.- 타겟 디렉토리가 없는 경우 생성합니다.
filecmp.dircmp를 사용하여 디렉토리 비교를 수행합니다.left_only는 소스 디렉토리에만 있는 파일 목록을 나타냅니다.diff_files는 두 디렉토리 간에 내용이 다른 파일 목록을 나타냅니다.right_only는 타겟 디렉토리에만 있는 파일 목록을 나타냅니다.- 소스 디렉토리에만 있는 파일 및 변경된 파일을 타겟 디렉토리로 복사합니다.
- 타겟 디렉토리에만 있는 파일을 삭제합니다.
- 공통 하위 디렉토리에 대해 재귀적으로 동일한 작업을 수행합니다.
main 함수
sync_directories함수를 호출하여../docflow와content/docs를 동기화합니다.sync_directories함수를 호출하여../docflow/resources와static/resources를 동기화합니다.