ChatGPT Python Task Automation Script Builder Prompt

Build complete, production-ready Python automation scripts for any repetitive task with full error handling and tests.

Category
💻 Coding
Difficulty
Intermediate
Models
3
Last Updated
2026-06-28
💻 Coding Intermediate python automation scripting task automation
Works with
📋 Prompt
You are a senior Python developer specialising in task automation.

Task: [detailed description of what to automate]
Input: [what data/files/sources the script receives]
Output: [what the script produces]
Environment: [local Mac/Windows/Linux/cloud]
Python: [3.10+/3.12/latest]

Write a complete production-ready script:
1. OVERVIEW: What it does, inputs, outputs, runtime estimate
2. COMPLETE CODE: Full imports, type hints, docstrings, inline comments, try/except for ALL external ops, logging, argparse if applicable, constants at top
3. DEPENDENCIES: requirements.txt + pip install command
4. USAGE: 3 command-line examples with expected output
5. TESTS: 3 pytest tests for core logic
6. EDGE CASES: What the script handles and what it doesn't
```python
#!/usr/bin/env python3
"""Markdown File Analyser — extracts metadata and exports summary CSV.
Usage: python md_analyser.py /path/to/folder [--output results.csv]"""
import argparse, csv, logging, re
from pathlib import Path
from typing import NamedTuple

logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
log = logging.getLogger(__name__)

class FileStats(NamedTuple):
filename: str; title: str; word_count: int; heading_count: int

def extract_title(content: str) -> str:
"""Extract first H1."""
m = re.search(r'^#\s+(.+)$', content, re.MULTILINE)
return m.group(1).strip() if m else 'Untitled'

def count_words(content: str) -> int:
"""Count words, excluding code blocks."""
return len(re.sub(r'```[\s\S]*?```', '', content).split())

def analyse_file(fp: Path) -> FileStats | None:
try:
c = fp.read_text(encoding='utf-8')
return FileStats(fp.name, extract_title(c), count_words(c),
len(re.findall(r'^#{1,6}\s', c, re.MULTILINE)))
except (OSError, UnicodeDecodeError) as e:
log.warning('Could not read %s: %s', fp, e); return None

def main() -> None:
p = argparse.ArgumentParser()
p.add_argument('folder', type=Path)
p.add_argument('--output', type=Path, default=Path('summary.csv'))
args = p.parse_args()
stats = [s for f in sorted(args.folder.glob('*.md')) if (s := analyse_file(f))]
with open(args.output, 'w', newline='') as f:
w = csv.writer(f)
w.writerow(['filename','title','word_count','heading_count'])
w.writerows(stats)
log.info('Saved %d rows to %s', len(stats), args.output)

if __name__ == '__main__': main()
```
DEPENDENCIES: stdlib only
USAGE: python md_analyser.py . | python md_analyser.py ./docs | python md_analyser.py ./docs --output out.csv
🏆
Best model for this prompt
DeepSeek
DeepSeek V3 / R1
💡 Pro Tips
Add --dry-run to scripts that modify or delete files — run without side effects first
Use pathlib.Path throughout — more readable and cross-platform than os.path
Log at INFO for normal ops, WARNING for recoverable issues, ERROR for failures
Put all configurable constants at the top — hardcoded values buried in functions are maintenance nightmares
⚠️ Common Mistakes
No error handling for file I/O — files can be missing, permissions fail, encoding can differ
Hardcoding paths like /home/user/docs — use argparse or Path.home() for portability
No progress indication for long-running scripts — add tqdm for batch operations
Mixing script logic with main() — keep functions small and testable; main() only wires them together
❓ FAQ 🔗 Related Prompts