Skip to main content

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

🔍 Quick Reference

ClassPurposeWhen to Use
PathPlatform-specific concrete pathsGeneral file operations
PurePathPlatform-specific pure pathsPath manipulation only
PosixPathPOSIX concrete pathsUnix/Linux file operations
WindowsPathWindows concrete pathsWindows file operations
PurePosixPathPOSIX pure pathsUnix/Linux path manipulation
PureWindowsPathWindows pure pathsWindows 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'