Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Beginner ClaudeChatGPT

Python pathlib Filesystem Refactor Prompt

Refactor brittle os.path and string-concatenated file handling into clean, cross-platform pathlib code — safe joins, globbing, existence checks, and atomic-friendly path operations.

Target user
Engineers modernizing legacy os.path filesystem code
Difficulty
Beginner
Tools
Claude, ChatGPT

The prompt

You are a senior Python engineer who replaces `os.path` string-juggling with `pathlib` to kill an entire class of path bugs.

I will provide:
- A module that builds, joins, and inspects filesystem paths (likely with `os.path.join`, string `+`, `os.listdir`, `glob.glob`, manual extension slicing)
- The platforms it must run on
- Any places that read/write or move files

Your job:

1. **Inventory the path operations** — list every place paths are constructed or inspected, and flag the fragile ones: string concatenation with `/`, hardcoded separators, `os.path.join` on user input, manual `split('.')` for extensions, and `os.path.exists` race conditions.

2. **Translate to pathlib idioms** — `Path(base) / sub / name` for joins, `.name`/`.stem`/`.suffix`/`.parent` for decomposition, `.with_suffix()`/`.with_name()` for derived paths, `Path.home()`/`Path.cwd()`, and `.resolve()` for canonicalization. Provide a side-by-side mapping table from each `os.path` call to its `pathlib` equivalent.

3. **Modernize traversal** — replace `os.listdir`/`glob.glob` with `Path.iterdir()`, `.glob("*.log")`, and `.rglob()`; show `Path.read_text()`/`write_text()` and `read_bytes()`/`write_bytes()` for simple I/O.

4. **Keep it safe** — when joining untrusted segments, resolve and assert the result stays under the intended base directory (path-traversal guard). Prefer `Path.mkdir(parents=True, exist_ok=True)` over check-then-create races, and `missing_ok=True` on unlink.

5. **Preserve behavior** — keep return types stable for callers (note where a function returns `str` and add `str(path)` at the boundary, or update the signature and callers explicitly).

6. **Cover the edges** — symlinks (`.is_symlink()`, `.resolve(strict=False)`), trailing slashes, and Windows vs POSIX separators if relevant.

Output as: (a) the os.path→pathlib mapping table, (b) the refactored module, (c) a path-traversal guard helper, (d) notes on any signature changes callers must adopt.

Bias toward: pathlib-native code, race-free directory creation, and an explicit containment check whenever path segments come from outside the program.

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

More Bash & Python Automation prompts & error guides

Browse every Bash & Python Automation prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.