Use Paths Without Guessing

A path names a location; it does not contain the file's data. Inspect the working directory and path evidence before changing code.

A filename such as report.txt does not tell us where Python will look for it. The location depends on the program's working directory. When a file cannot be found—or appears in an unexpected folder—we should inspect the path rather than try random prefixes.

Build Paths as Paths

Python's standard library includes Path, a tool for working with filesystem locations. This import makes that tool available in the current file:

from pathlib import Path

We can then name the input and output locations by their roles:

The / operator joins path parts here; it is not numerical division. Path uses the form expected by the operating system, so we do not need to assemble the separators by hand.

Both paths above are relative. They begin from the process's current working directory. An absolute path identifies a location from the filesystem's root. We can ask rather than infer:

Path.cwd() displays the current working directory. resolve() shows the absolute location represented by a relative path in that directory. These are diagnostic facts: they tell us where Python is looking.

Q1. Locate a relative input path

The working directory is /course/python, and the program opens Path("data") / "measurements.txt". Which location does it mean?

Choose one

Select one choice, then check.

HintStart from the working directory

The path has no filesystem root at its beginning.

SolutionResolve from the current directory

Starting from /course/python, append data/measurements.txt. The resulting location is /course/python/data/measurements.txt.

Not attempted
Review

Not marked done.

Inspect What Exists

Before opening an important input, inspect the location and the kind of entry found there:

These questions are different. A path may not exist. It may exist but name a directory when the program expects a file. Checking only exists() would not distinguish those cases.

Suppose the output says:

exists: True
is file: False
is directory: True

The path exists, but opening it as a measurement file is still the wrong operation. This evidence is more useful than repeatedly changing the filename.

Q2. Interpret a path inspection

A path reports exists() == True, is_file() == False, and is_dir() == True. What has the program found?

Choose one

Select one choice, then check.

HintUse the most specific true check

is_dir() is true while is_file() is false.

SolutionThe location is a directory

The entry exists and is a directory. The program should not treat it as the measurement text file.

Not attempted
Review

Not marked done.

Create Output Directories, Not Missing Inputs

An output directory belongs to this program, so the program may create it:

parents=True allows missing parent output directories to be created when the chosen path has more than one level. exist_ok=True allows the directory to already exist. The call creates directories, not the report file itself; the later write creates that file.

A missing input is different. If data/measurements.txt is supposed to arrive from a previous step or another source, silently creating an empty replacement would erase useful evidence. Inspect and report the missing input instead.

RoleMissing pathAppropriate response
required input filedata/measurements.txtstop and diagnose its location
program-owned output directoryreports/create the directory

Q3. Prepare only the output location

Complete the program so it creates reports/figures/ and constructs the report path without creating a missing input location.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintCreate the directory before joining the filename

Call reports_dir.mkdir(parents=True, exist_ok=True). Then use / to join "summary.txt" to reports_dir.

SolutionCreate reports, preserve missing-input evidence

The program creates only its output directory. It does not create data/measurements.txt or disguise its absence.

Not attempted
Review

Not marked done.

Treat a path as a location, not as file content. Resolve relative paths from the visible working directory, inspect existence and entry type, and create program-owned output directories without fabricating required input. With locations explicit, parsing and computation can move into reusable code.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.