Fix the Path Joining Bug
Intermediate · Fix the bug · 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 lessonThis code tries to build a path by concatenating strings, but it produces an incorrect result. Fix it to use pathlib's / operator.
Expected output:
/home/user/docs/notes.txtfrom pathlib import PurePosixPath
base = PurePosixPath('/home/user')
result = str(base) + 'docs' + 'notes.txt'
print(result)
More pathlib exercises
- Extract Path ComponentsBeginner
- Predict the Joined PathBeginner
- Change File ExtensionIntermediate
- Filter Files by ExtensionAdvanced
All Python exercises · 50 practice problems · File I/O & Data Formats quiz