Answer key · 12 questions

Python Paths with pathlib Exercises With Answers

All 12 questions for Python Paths with pathlib in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

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.

01 · Multiple choice

What does Path('data') / 'scores.csv' do?

  1. Reads the file
  2. Joins two path partscorrect
  3. Deletes the file
  4. Divides two numbers

Answer: Joins two path parts

Why: Path overloads the slash operator to join path components.

02 · Multiple choice

What is Path('a/b.txt').name?

  1. b.txtcorrect
  2. .txt
  3. a
  4. a/b

Answer: b.txt

Why: name is the final component of the path.

03 · Multiple choice

What is the suffix of Path('report.tar.gz')?

  1. report
  2. .tar
  3. .gzcorrect
  4. tar.gz

Answer: .gz

Why: suffix returns only the last suffix, including its dot.

04 · Multiple choice

Why pass encoding='utf-8' to write_text?

  1. To create a folder
  2. To make the file smaller
  3. So the file bytes do not depend on the machine localecorrect
  4. To convert the path to a string

Answer: So the file bytes do not depend on the machine locale

Why: An explicit encoding makes text files portable across systems.

05 · Fill in the blank

Complete the path join.

from pathlib import Path
path = Path("data") ____ "scores.csv"

Answer: /

Why: The slash operator joins Path parts.

06 · Fill in the blank

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.

07 · Fill in the blank

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.

08 · Fill in the blank

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.

09 · Predict output

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.

10 · Predict output

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.

11 · Predict output

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.

12 · Predict output

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.

Practice this lesson