Extract Path Components
Beginner · Write the code · from Python pathlib: Modern File Path Handling
From the lesson: Python pathlib: Modern File Path Handling
Imagine navigating to a friend's house. You could follow vague paper directions like "turn left at the big tree, then right at the red mailbox." Or you could use GPS, which understands addresses, calculates routes, and handles all the tricky details for you.
Read the lessonGiven the path /home/user/documents/essay.txt, create a PurePosixPath and print the file name, the suffix (extension), and the parent directory, each on its own line.
Expected output:
essay.txt
.txt
/home/user/documentsfrom pathlib import PurePosixPath
# Your code here
More pathlib exercises
- Predict the Joined PathBeginner
- Change File ExtensionIntermediate
- Fix the Path Joining BugIntermediate
- Filter Files by ExtensionAdvanced
All Python exercises · 50 practice problems · File I/O & Data Formats quiz