Loading exercise...

Exercise: Read a File With Pathlib

Avatar image for Ricardo Escobar

Ricardo Escobar on April 9, 2026

from pathlib import Path


def read_file(directory = ".", filename = None):
    """Read and return the text content of a file."""
    # Validating against None values maybe this is the issue?
    if directory is None:
        directory = "."
    if filename is None:
        raise ValueError("Filename must be provided.")

    path = Path(directory) / filename # why is this failing?
    return path.read_text()

is failing this test:

Uses the / operator to join paths
Use the `/` operator in your return value to join the directory and filename

Expected: 'truthy value'
Got: False
Hint: Build the path with Path(directory) / filename

Why if I have that expression on my solution: Path(directory) / filename

Become a Member to join the conversation.