Python Bulk File Processing Script Prompt
Build a safe, idempotent batch file-processing script in Python that walks directories, transforms or renames files atomically, handles huge datasets without exhausting memory, and can resume after interruption.
- Target user
- Engineers automating bulk file transforms, renames, or migrations
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a data-plumbing engineer who has processed millions of files and learned every way a bulk job can corrupt data or run out of memory. I will provide: - The transform (convert, rename, compress, extract, dedupe, move) - Source layout and rough scale (count, total size, file sizes) - Whether the job may re-run and whether sources are mutable Build a robust batch processor: 1. **Streaming, not slurping** — walk with `os.scandir`/`pathlib` lazily and process files one at a time or in bounded batches; for large individual files, read in chunks so memory stays flat regardless of dataset size. Never build a giant in-memory list of every path when count is huge. 2. **Atomic writes** — write to a temp file in the same directory then `os.replace()` onto the target so an interrupted run never leaves a half-written or corrupt output; never edit in place without a temp+rename. 3. **Idempotency + resume** — make re-running safe: skip files already processed (check a marker, an output existence, or a manifest of done items). Maintain a manifest/checkpoint so an interrupted job resumes instead of redoing everything. 4. **Edge cases** — filenames with spaces/unicode/newlines, symlinks (follow or not — be explicit), permission errors, zero-byte files, and name collisions on rename. Handle each deliberately, don't crash on the first weird file. 5. **Dry-run + per-file error isolation** — `--dry-run` prints planned actions; in apply mode, a failure on one file logs and continues (configurable `--fail-fast`) rather than aborting the whole batch. 6. **Parallelism (only if it helps)** — for CPU-bound transforms use `ProcessPoolExecutor`; for IO-bound use threads or async; cap workers and explain when parallelism just thrashes the disk and is slower. 7. **Progress + reporting** — a progress indicator for long runs and a final summary: processed, skipped, failed, bytes handled. 8. **Testing** — pytest with `tmp_path` fixtures covering resume-after-interrupt, a collision, and a permission error. Output: (a) the processor script with argparse, (b) the manifest/checkpoint logic, (c) the atomic-write helper, (d) pytest tests, (e) example dry-run output. Bias toward: atomic writes, resumability, flat memory, never corrupt source data.
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
-
Bash Word-Splitting and Quoting Hardening Prompt
Audit and rewrite a Bash script to eliminate unquoted-expansion bugs, unsafe word splitting, and glob injection while preserving intended behavior
-
Bash Exit-Code and Pipefail Propagation Audit Prompt
Audit a Bash script for swallowed failures, missing pipefail, and broken exit-code propagation through pipes, subshells, and command substitution
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
-
Bash Sourceable Function Library Pattern Prompt
Refactor scattered Bash helpers into a safe, namespaced, sourceable library that can be reused across scripts without polluting the caller's environment
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.