Agent skill

pathlib

Object-oriented filesystem paths

Stars 163
Forks 31

Install this agent skill to your Project

npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/fixtures-skilldoai-skilldo-2

SKILL.md

Imports

python
from pathlib import Path
import os

Core Patterns

Basic Path Creation

Create and manipulate filesystem paths.

python
from pathlib import Path

# Create a path
p = Path('/tmp/example.txt')

# Check if it exists
if p.exists():
    print(f"File exists: {p}")

# Get parts
print(f"Name: {p.name}")
print(f"Parent: {p.parent}")

Reading Files

Read file contents using Path.

python
from pathlib import Path

p = Path('/tmp/example.txt')

# Write
p.write_text('Hello World!')

# Read
content = p.read_text()
print(f"Content: {content}")

Directory Iteration

Iterate over files in a directory.

python
from pathlib import Path

# List all Python files
for file in Path('.').glob('*.py'):
    print(f"Found: {file.name}")

Configuration

Path objects are immutable and work consistently across platforms.

Pitfalls

Wrong: Using string concatenation for paths

python
path = '/tmp' + '/' + 'file.txt'  # Platform-specific

Right: Use Path division operator

python
from pathlib import Path
path = Path('/tmp') / 'file.txt'  # Cross-platform

References

Expand your agent's capabilities with these related and highly-rated skills.

Didn't find tool you were looking for?

Be as detailed as possible for better results