Challenge - 5 Problems
Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of joining paths?
Consider the following Python code using
os.path.join. What will be printed?Python
import os path = os.path.join('folder', 'subfolder', 'file.txt') print(path)
Attempts:
2 left
💡 Hint
Think about how paths are joined on Unix-like systems.
✗ Incorrect
The
os.path.join function joins parts of a path using the system's path separator. On Unix-like systems, it uses forward slashes (/). So the output will show forward slashes between parts.❓ Predict Output
intermediate2:00remaining
What does os.path.basename return?
What will the following code print?
Python
import os filename = os.path.basename('/home/user/docs/report.pdf') print(filename)
Attempts:
2 left
💡 Hint
Think about what the basename function extracts from a path.
✗ Incorrect
os.path.basename returns the last part of the path, which is the file name with extension.❓ Predict Output
advanced2:00remaining
What is the output of normpath with redundant separators?
What will this code print?
Python
import os path = os.path.normpath('folder//subfolder/../file.txt') print(path)
Attempts:
2 left
💡 Hint
Think about how
normpath simplifies paths by removing redundant parts.✗ Incorrect
os.path.normpath removes redundant separators and resolves '..' to go up one directory, so 'folder//subfolder/../file.txt' becomes 'folder/file.txt'.❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this code raise?
Python
import os path = os.path.join('folder', None, 'file.txt') print(path)
Attempts:
2 left
💡 Hint
Consider what happens when you pass None to os.path.join.
✗ Incorrect
Passing None to
os.path.join causes a TypeError because it expects strings, bytes, or os.PathLike objects, not NoneType.🧠 Conceptual
expert2:00remaining
How many items are in the list after splitting a path?
Given this code, how many items will the list
parts contain?Python
import os path = '/usr/local/bin/python3' parts = path.split(os.sep) print(len(parts))
Attempts:
2 left
💡 Hint
Remember that splitting by '/' on an absolute path creates an empty string at the start.
✗ Incorrect
Splitting '/usr/local/bin/python3' by '/' results in ['', 'usr', 'local', 'bin', 'python3'], which has 5 items.