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.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Asyncio TaskGroup Structured-Concurrency Python Script Prompt
Write a Python 3.11+ automation script that fans work out with asyncio.TaskGroup so failures cancel siblings cleanly, errors aggregate into an ExceptionGroup, and no task is ever silently orphaned.
-
Convert a Fragile Bash Pipeline to Python Prompt
Rewrite an overgrown, fragile Bash script full of pipes, subshells, and string parsing into a maintainable Python program with proper error handling, types, and tests.
-
Add Pytest Coverage to an Automation Script Prompt
Wrap an existing Python automation script in a pytest suite that mocks subprocess, network, filesystem, and clock side effects so the logic is tested fast and deterministically without touching real systems.
-
Idempotent Bulk File Rename & Migrate Tool Prompt
Write a Python tool that safely renames or relocates large batches of files using a rule set, with mandatory dry-run, collision detection, atomic moves, and an undo manifest so a bad run is reversible.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.