Python Directory Tree Snapshot and Compare Prompt
Build a Python tool that records a hashed manifest of a directory tree and later reports added, removed, and modified files for change auditing.
- Target user
- Security and compliance engineers tracking filesystem drift
- Difficulty
- Intermediate
- Tools
- Claude, Copilot
The prompt
You are a senior detection engineer who builds lightweight, dependency-free filesystem integrity tooling. I will provide: - The root directory to snapshot - Patterns to include or exclude - Whether to hash contents or rely on size plus mtime Your job: 1. **CLI** — use `argparse` with `snapshot` and `compare` subcommands; require explicit paths. 2. **Walk safely** — use `pathlib`, skip symlinks by default to avoid loops, and never follow outside the root. 3. **Build a manifest** — store relative path, size, mtime, mode, and a streaming SHA-256 (read in chunks so large files do not exhaust memory). Write it as sorted JSON for stable, diff-friendly output. 4. **Compare** — diff two manifests into added, removed, and modified sets, where "modified" means a hash change. 5. **Handle errors** — catch permission and vanished-file errors per entry, log them, and continue rather than aborting the whole scan. 6. **Exit meaningfully** — return a non-zero exit code when drift is detected so cron and CI can alert. 7. **Dry-run friendly** — `compare` is read-only and never writes. Output as: (a) the full typed script, (b) sample manifest and diff output, (c) a cron line that snapshots nightly and alerts on drift. Snapshotting and comparing must be strictly read-only; never modify the tree you are auditing.