Skip to content
python

Write a robust Python automation script with error handling

Produces a Python automation script that handles real-world edge cases — bad input, network failures, partial completion — instead of a fragile happy-path-only script.

Who should use this

Is this prompt for you?

  • Developers automating a repetitive task (file processing, API polling, data cleanup)
  • Anyone who has been burned by a Python script that crashed halfway through a batch job
  • People who want a script they can actually hand off or schedule as a cron job
The prompt

Copy this prompt

Prompt
Write a Python script that does the following: [describe the task precisely, e.g. "reads all CSV files in a folder, validates required columns, merges them into one deduplicated CSV, and logs any rows it had to skip"].

Environment:
- Python version: [e.g. 3.11]
- Allowed dependencies: [list any you want to use, or say "standard library only" / "pandas is fine"]
- How it will run: [manually / cron job / CI pipeline / triggered by another script]

Requirements:
1. Use a proper CLI interface (argparse or click) with --help text, not hardcoded paths
2. Validate all inputs before doing real work (file exists, required columns present, correct types) and fail with a clear error message rather than a raw traceback
3. Handle partial failures gracefully — if one file/record fails, log it and continue rather than crashing the whole run, unless I say otherwise
4. Use the logging module (not print) with an adjustable log level
5. Make it idempotent where possible — running it twice on the same input should not corrupt output or duplicate data
6. Add type hints throughout and docstrings on every function
7. Write pytest tests covering: normal input, empty input, malformed input, and at least one failure-recovery case
8. Include a short usage example in a comment at the top of the file

After writing it, tell me what assumptions you made about the input format, and what I should double check before running this against real data.

Works well with Claude Code, OpenAI Codex and Cursor.

How to use it

Getting the best result

  1. 1Be precise about the exact task and input/output format — vague descriptions produce scripts that don't match your real data
  2. 2Specify whether partial failures should skip-and-continue or stop-and-fail; this changes the whole error-handling design
  3. 3Run the generated pytest suite before running the script against real production data
  4. 4Test with a small, disposable sample of real data first, not your full dataset
Expected result

What you should get back

  • A CLI-driven Python script with input validation, logging, and graceful error handling
  • Pytest tests covering normal, empty, malformed, and failure-recovery cases
  • A clear list of assumptions made about your input data
  • A script that fails loudly and clearly instead of silently corrupting output
Tips

Get more out of this prompt

  • Always ask for idempotency if the script writes files or calls an API — reruns are inevitable
  • Ask it to log skipped/failed records to a separate file so a big batch job is auditable afterward
  • Request type hints and docstrings even for "quick" scripts — you will reread this in six months
Common mistakes

What to watch out for

  • Not specifying failure behavior, resulting in a script that crashes on the first bad record in a large batch
  • Running the untested script directly against production data instead of a small sample first
  • Accepting print-based debugging output instead of asking for proper logging with levels

Ready to put this prompt to work?

Pair it with an AI coding tool and a real project, then ship something this week.