Loading exercise...

Exercise: Join Path and Read

Avatar image for DimaG

DimaG on April 4, 2026

Here is the another version of the same function using pathlib.Path:

def join_and_read(*parts):
    """Join path parts and return the file's content."""
    with Path(*parts).open() as f:
        return f.read()

path = Path.home()
print(join_and_read(path, "some_directory", "data_01.txt"))
print(join_and_read(path, "some_directory", "sub_dir", "file1.txt"))        
Avatar image for DimaG

DimaG on April 4, 2026

The output for the first call is: This is a test text file number 1

The output of the second call: This is test text file number 1

Become a Member to join the conversation.