Problem Statement
When would you prefer pathlib over os.path? Explain the benefits.
Explanation
pathlib provides object-oriented paths with clear operators and methods. Code becomes more readable, such as path slash 'file.txt' instead of string joins. It also handles Windows and POSIX differences for you.
Paths expose high-level actions like read_text, read_bytes, iterdir, and rename. This reduces boilerplate and avoids mistakes with separators and encodings.
Code Solution
SolutionRead Only
from pathlib import Path
p = Path('logs')/'app.log'
text = p.read_text(encoding='utf-8')