This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Paths with pathlib, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
What does Path('data') / 'scores.csv' do?
Answer: Joins two path parts
Why: Path overloads the slash operator to join path components.
What is Path('a/b.txt').name?
Answer: b.txt
Why: name is the final component of the path.
What is the suffix of Path('report.tar.gz')?
Answer: .gz
Why: suffix returns only the last suffix, including its dot.
Why pass encoding='utf-8' to write_text?
Answer: So the file bytes do not depend on the machine locale
Why: An explicit encoding makes text files portable across systems.
Complete the path join.
from pathlib import Path
path = Path("data") ____ "scores.csv"Answer: /
Why: The slash operator joins Path parts.
Complete the property that returns '.txt'.
from pathlib import Path
print(Path("notes.txt").____)Answer: suffix
Why: suffix is the final extension, including the dot.
Complete the property that returns the containing folder.
from pathlib import Path
print(Path("reports/2026.txt").____.as_posix())Answer: parent
Why: parent gives the directory that contains the path.
Complete the encoding argument.
from pathlib import Path
Path("note.txt").write_text("hi", encoding="____")Answer: utf-8
Why: Use the standard UTF-8 name with a hyphen.
What does this code print?
from pathlib import Path
print(Path("reports/2026.txt").stem)Answer: 2026
Why: stem removes the final suffix from the file name.
What does the program output?
from pathlib import Path
print(Path("reports/2026.txt").suffix)Answer: .txt
Why: The suffix includes the dot before txt.
What appears on the screen?
from pathlib import Path
print(Path("a/b/c.csv").parent.as_posix())Answer: a/b
Why: parent removes the file name, and as_posix uses forward slashes.
What is printed?
from pathlib import Path
print((Path("a") / "b.txt").as_posix())Answer: a/b.txt
Why: The slash operator joins the parts, and as_posix normalizes the separator.
Try the questions first
Open the practice page for Python Paths with pathlib and answer the same 12 questions with instant feedback.