🧩 Python Automation Recipes – 📂 Folder Sync Checker
Posted on: March 30, 2026
Description:
📌 Introduction
When you keep copies of files across two folders — like local backups, mirrored directories, or project sync locations — it’s easy for them to drift apart without noticing.
This automation recipe shows how to compare two folders and detect missing or changed files using Python. It’s useful for validating backups, checking sync status, and making sure important files stay aligned across locations.
🔎 Explanation
- The script scans two folders recursively.
- It builds a map of files using their relative paths, so matching files can be compared correctly even in nested directories.
- For each file, it checks:
- whether the file is missing from one side
- whether the file exists in both places but has different contents
- File differences are detected using a SHA-256 hash, which compares actual content instead of just file size or filename.
This makes the script reliable for backup checks and sync validation workflows.
✅ Key Takeaways
- 📂 Compare two folders recursively and detect mismatches.
- 🔍 Find missing files and changed files accurately.
- ⚙️ Useful for backups, sync audits, and folder validation.
Code Snippet:
from pathlib import Path
import hashlib
# --- Step 1: Configure folders ---
# First folder to compare
SOURCE_FOLDER = Path("") # change this
# Second folder to compare
TARGET_FOLDER = Path(" str:
"""
Generate a SHA-256 hash for a file's contents.
Files with the same content will produce the same hash.
"""
hasher = hashlib.sha256()
with file_path.open("rb") as f:
while chunk := f.read(8192):
hasher.update(chunk)
return hasher.hexdigest()
# --- Step 3: Build relative file maps for both folders ---
source_files = {
file.relative_to(SOURCE_FOLDER): file
for file in SOURCE_FOLDER.rglob("*")
if file.is_file()
}
target_files = {
file.relative_to(TARGET_FOLDER): file
for file in TARGET_FOLDER.rglob("*")
if file.is_file()
}
# --- Step 4: Compare file sets ---
all_relative_paths = set(source_files.keys()) | set(target_files.keys())
missing_in_target = []
missing_in_source = []
changed_files = []
for rel_path in sorted(all_relative_paths):
source_file = source_files.get(rel_path)
target_file = target_files.get(rel_path)
# File exists in source but not in target
if source_file and not target_file:
missing_in_target.append(rel_path)
# File exists in target but not in source
elif target_file and not source_file:
missing_in_source.append(rel_path)
# File exists in both; compare contents
else:
if get_file_hash(source_file) != get_file_hash(target_file):
changed_files.append(rel_path)
# --- Step 5: Print results ---
print("Folder Sync Check Report\n")
if missing_in_target:
print("Missing in TARGET_FOLDER:")
for path in missing_in_target:
print(f" - {path}")
print()
if missing_in_source:
print("Missing in SOURCE_FOLDER:")
for path in missing_in_source:
print(f" - {path}")
print()
if changed_files:
print("Changed files:")
for path in changed_files:
print(f" - {path}")
print()
if not missing_in_target and not missing_in_source and not changed_files:
print("Both folders are fully in sync.")
Link copied!
Comments
Add Your Comment
Comment Added!
No comments yet. Be the first to comment!