pathlib - Object-oriented Filesystem Paths
The pathlib module provides an object-oriented approach to handling filesystem paths. It's the recommended way to work with paths in modern Python code.
📦 Path Classes
- pathlib-path - Concrete path class for the current platform
- pathlib-purepath - Pure path manipulation without I/O
- pathlib-posixpath - POSIX-specific path operations
- pathlib-pureposixpath - Pure POSIX path manipulation
- pathlib-windowspath - Windows-specific path operations
- pathlib-purewindowspath - Pure Windows path manipulation
🔍 Quick Reference
| Class | Purpose | When to Use |
|---|---|---|
Path | Platform-specific concrete paths | General file operations |
PurePath | Platform-specific pure paths | Path manipulation only |
PosixPath | POSIX concrete paths | Unix/Linux file operations |
WindowsPath | Windows concrete paths | Windows file operations |
PurePosixPath | POSIX pure paths | Unix/Linux path manipulation |
PureWindowsPath | Windows pure paths | Windows path manipulation |
🚀 Basic Usage
from pathlib import Path
# Create a path
p = Path('documents/script.py')
# Check if file exists
if p.exists():
print(f"File size: {p.stat().st_size}")
# Get parent directory
parent = p.parent
# Join paths
config_file = p.parent / 'config.ini'