Challenge - 5 Problems
File 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 this code using pathlib?
Consider the following Python code that uses
pathlib to manipulate file paths. What will be printed?Python
from pathlib import Path p = Path('/home/user/docs') new_path = p / 'projects' / '..' / 'notes.txt' print(new_path.resolve())
Attempts:
2 left
💡 Hint
Remember that '..' means to go up one directory, and
resolve() simplifies the path.✗ Incorrect
The
resolve() method simplifies the path by resolving '..' to go up one directory. So, '/home/user/docs/projects/../notes.txt' becomes '/home/user/docs/notes.txt'.❓ Predict Output
intermediate2:00remaining
What does this os.path code output?
Given this Python code using
os.path, what will be the output?Python
import os path = '/var/log/syslog' print(os.path.basename(os.path.dirname(path)))
Attempts:
2 left
💡 Hint
os.path.dirname() gets the folder path, and os.path.basename() gets the last part of a path.✗ Incorrect
First,
os.path.dirname('/var/log/syslog') returns '/var/log'. Then, os.path.basename('/var/log') returns 'log'.❓ Predict Output
advanced2:00remaining
What is the output of this code using pathlib's parts?
Look at this code that uses
pathlib.Path.parts. What will it print?Python
from pathlib import Path p = Path('/usr/local/bin/python3') print(p.parts[2])
Attempts:
2 left
💡 Hint
The
parts attribute splits the path into a tuple starting with root '/' as the first part.✗ Incorrect
The parts tuple is ('/', 'usr', 'local', 'bin', 'python3'). Index 2 is 'local'.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error will this Python code raise when run?
Python
import os path = '/tmp/testfile.txt' print(os.path.join(path, 'subdir'))
Attempts:
2 left
💡 Hint
Check how
os.path.join works when the first argument is an absolute path.✗ Incorrect
os.path.join appends the second argument to the first path. Since the first is absolute, it joins as expected.🧠 Conceptual
expert3:00remaining
Which option correctly normalizes a Windows path?
You have a Windows path string with mixed slashes:
"C:/Users\\Admin/../Documents\\file.txt". Which option correctly normalizes it to a clean absolute path using Python?Attempts:
2 left
💡 Hint
Normalization fixes slashes and removes '..' parts without requiring the file to exist.
✗ Incorrect
os.path.normpath cleans the path string by fixing slashes and resolving '..' without checking the file system. Path.resolve() requires the path to exist and may raise errors.