Skip to content
Foxipa
Code Completion 15 Challenges

Complete the Python paths and pathlib code

Practice composing, searching, comparing, normalizing, expanding, splitting, and converting filesystem paths with pathlib and selected os.path functions, including drive/root/tail decomposition, path identity, environment-variable expansion, and str/bytes interoperability.

Intermediate ~18 min read

Challenge 1

Build the path project/src/main.py.

python Your answer
from pathlib import Path

source = 

print(source)

Press Ctrl⌘Enter to check your answer

Challenge 2

Get the .csv suffix from the path.

python Your answer
from pathlib import Path

path = Path('reports') / 'summary.csv'
suffix = 

print(suffix)

Press Ctrl⌘Enter to check your answer

Challenge 3

Create a new Path whose suffix is .json.

python Your answer
from pathlib import Path

source = Path('reports') / 'summary.csv'
json_path = 

print(json_path)

Press Ctrl⌘Enter to check your answer

Challenge 4

Check whether report is a file.

python Your answer
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as folder:
    report = Path(folder) / 'report.txt'
    report.touch()
    result = 
    print(result)

Press Ctrl⌘Enter to check your answer

Challenge 5

List only the immediate entries in root, sorted by entry name. Do not include files nested inside subdirectories.

python Your answer
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temporary:
    root = Path(temporary)
    (root / "archive").mkdir()
    (root / "notes.txt").touch()
    (root / "archive" / "nested.txt").touch()

    entries = sorted(root.(), key=lambda path: path.name)
    print([entry.name for entry in entries])

Press Ctrl⌘Enter to check your answer

Challenge 6

Find CSV files in root and every subdirectory, displaying each match relative to root in sorted order.

python Your answer
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temporary:
    root = Path(temporary)
    (root / "nested").mkdir()
    (root / "root.csv").touch()
    (root / "nested" / "inside.csv").touch()

    csv_files = sorted(
        path.relative_to(root).as_posix()
        for path in root.("*.csv")
    )
    print(csv_files)

Press Ctrl⌘Enter to check your answer

Challenge 7

Find the common path by complete components, not by matching characters.

python Your answer
import os

paths = [
    os.path.join("workspace", "reports", "current.csv"),
    os.path.join("workspace", "reports-old", "archive.csv"),
]
shared = 
print(shared)

Press Ctrl⌘Enter to check your answer

Challenge 8

Expand the leading ~ in the requested path explicitly.

python Your answer
from pathlib import Path

requested = Path("~/reports/today.csv")
expanded = 
print(expanded.is_absolute())
print(expanded.name)

Press Ctrl⌘Enter to check your answer

Challenge 9

Make the candidate absolute and remove its .. component while resolving supported symbolic links.

python Your answer
from pathlib import Path

candidate = Path.cwd() / "folder" / ".." / "report.txt"
resolved = 
print(".." in resolved.parts)

Press Ctrl⌘Enter to check your answer

Challenge 10

Compute a relative route from base to target even though target is outside base.

python Your answer
from pathlib import Path
import os

base = Path.cwd() / "project"
target = base.parent / "shared" / "data.csv"
relative = 
print(Path(relative).parts[0] == "..")

Press Ctrl⌘Enter to check your answer

Challenge 11

Check whether two path spellings refer to the same existing file. Do not compare their strings or contents.

python Your answer
import os
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temporary:
    root = Path(temporary)
    (root / "nested").mkdir()
    report = root / "report.txt"
    report.write_text("data", encoding="utf-8")
    alternate = os.path.join(str(root), "nested", "..", "report.txt")
    same = 
    print(same)

Press Ctrl⌘Enter to check your answer

Challenge 12

Normalize redundant separators and . / .. components lexically, without resolving symbolic links.

python Your answer
import os

raw = os.path.join("reports", "2026", "..", "summary.csv")
normalized = 
print(normalized == os.path.join("reports", "summary.csv"))

Press Ctrl⌘Enter to check your answer

Challenge 13

Decode a bytes path produced by os.fsencode() back into a str path using the filesystem encoding and error handler.

python Your answer
import os
from pathlib import Path
from tempfile import TemporaryDirectory

with TemporaryDirectory() as temporary:
    path = Path(temporary) / "report.txt"
    path.touch()
    encoded = os.fsencode(path)
    restored = 
    print(restored == str(path))
    print(type(encoded).__name__)

Press Ctrl⌘Enter to check your answer

Challenge 14

Expand environment-variable references in a path string with os.path. Keep the demonstration deterministic by temporarily setting FOXIPA_REPORTS_ROOT.

python Your answer
import os
from unittest.mock import patch

with patch.dict(os.environ, {"FOXIPA_REPORTS_ROOT": "workspace/reports"}, clear=True):
    requested = "$FOXIPA_REPORTS_ROOT/summary.csv"
    expanded = 
    print(expanded)

Press Ctrl⌘Enter to check your answer

Challenge 15

Use ntpath.splitroot() to separate this Windows UNC path into drive/share, root separator, and the remaining tail. Preserve the original path spelling.

python Your answer
import ntpath

path = r"\\server\share\logs\app.log"
drive, root, tail = 
print(repr(drive))
print(repr(root))
print(repr(tail))
print(drive + root + tail == path)

Press Ctrl⌘Enter to check your answer