heic_sync/sync.py

68 lines
2.2 KiB
Python

import os
import shutil
from pathlib import Path
import sys
import argparse
def sync_files(src_path: str, dest_path: str) -> None:
"""
Synchronize files from source to destination directory, skipping existing files.
Args:
src_path: Source directory path
dest_path: Destination directory path
"""
# Convert to Path objects for easier handling
src = Path(src_path)
dest = Path(dest_path)
# Ensure source directory exists
if not src.exists():
raise FileNotFoundError(f"Source directory {src_path} does not exist")
# Create destination directory if it doesn't exist
dest.mkdir(parents=True, exist_ok=True)
# Walk through source directory
for root, _, files in os.walk(src):
# Convert current root to Path object
root_path = Path(root)
# Calculate relative path from source
rel_path = root_path.relative_to(src)
# Create corresponding destination directory
dest_dir = dest / rel_path
dest_dir.mkdir(parents=True, exist_ok=True)
# Copy each file
for file in files:
src_file = root_path / file
dest_file = dest_dir / file
# Skip if destination file exists
if dest_file.exists():
print(f"Skipping existing file: {dest_file}")
continue
# Copy the file
print(f"Copying: {src_file} -> {dest_file}")
shutil.copy2(src_file, dest_file)
# Usage
# python sync.py "C:\Users\petr\OneDrive\Photos\Auto-saved\2024\11" "D:\Photos\OneDrive-Photo\2024\11"
# python sync.py /path/to/source /path/to/destination
if __name__ == "__main__":
# Set up argument parser
parser = argparse.ArgumentParser(description='Synchronize files from source to destination directory')
parser.add_argument('src', help='Source directory path')
parser.add_argument('dest', help='Destination directory path')
# Parse arguments
args = parser.parse_args()
try:
sync_files(args.src, args.dest)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)