The os.path module uses string-based operations while pathlib provides object-oriented path handling. pathlib is more modern, readable, and cross-platform compatible.
Key differences:
os.path: String-based, requires multiple function calls
pathlib: Object-oriented, chainable methods, more intuitive
# os.path approach
import os
path = os.path.join('home', 'user', 'documents', 'file.txt')
if os.path.exists(path):
size = os.path.getsize(path)
# pathlib approach
from pathlib import Path
path = Path('home') / 'user' / 'documents' / 'file.txt'
if path.exists():
size = path.stat().st_size
Rewriting in plainer words…
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.