Files and Paths with Pathlib
Talk it through out loud with your AI coach.
The problem
- easy
- Pro
An old script builds file paths by gluing strings together with forward slashes, and it breaks on a colleague's Windows machine. Explain how Python's pathlib module would handle paths instead, and show how you'd find every CSV file in a folder and its subfolders.
What a strong answer covers
- Why treating paths as plain strings causes cross-platform bugs.
- How pathlib models a path and joins its parts.
- Reading the pieces of a path, such as its extension or folder.
- Searching a whole folder tree for files of one kind.
Common mistake: Swapping the hard-coded slashes for backslashes, which just moves the bug to Mac and Linux.
Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.
Practise it out loud