COGNITION FOUNDRYLEARN. BUILD. UNDERSTAND.
PY-023 / Represent locations correctly◷ 22 min read + practice

Paths with pathlib

Paths are not just strings with slashes. Python’s pathlib supplies objects that understand path components and make many path operations easier to read and maintain.

By the end, you can: Use PureWindowsPath to analyse Windows-style paths and Path when actual local files are required.
Before this lesson: PY-021 · PY-022

Choose the path class for the job

PureWindowsPath understands Windows-style path syntax even when the Python program is running on a non-Windows computer. It manipulates path names only and does not access disks.

The class Path represents paths for the operating system actually executing Python. Use it when your program needs to read or write real files, and obtain appropriate permission first.

Ask for components instead of slicing text

path.name gives the final component; path.suffix gives the final extension; path.parent gives the parent path. These operations are more intentional than guessing where the final backslash appears.

A path may have no suffix, several dots or trailing separators. Check the result instead of assuming every name is a simple base.ext string.

Join paths without hand-built separators

The / operator between path objects joins parts. PureWindowsPath("C:/Projects") / "notes" / "file.txt" constructs a Windows-style path regardless of the browser host system.

Avoid simply concatenating strings and manually adding slashes. Use pathlib for structure and keep display formatting a separate decision.

Browser safety and actual files

A PureWindowsPath example does not prove that a file exists. The browser cannot enumerate your Windows directories. In a local Python program, Path can use exists() or open files subject to permissions and your program’s safety checks.

Pure path analysis stays separate from filesystem operations. An example that displays a filename or parent directory cannot confirm whether the file exists, and it does not need access to the visitor’s computer.

Try it yourself.

Choose a starting example or write something completely different. You control the code; the Run button executes what is in the editor, not a hidden preset.

CF Python PlaygroundBrowser-based Python
Ready to load Python
Inspect a Windows path

Read the filename, final suffix and parent using PureWindowsPath.

Syntax colours highlight Python code. Tab inserts four spaces; press Escape then Tab to leave the editor. Ctrl+Enter runs the current code.
OUTPUT / ERRORS
Press Run Python to execute the editable code.
NOW MAKE IT YOURS

Change the filename to report.csv and compare the suffix.

Need a hint?

Only change the last component of the raw Windows path string.

On first Run, a pinned Python runtime downloads from jsDelivr. Code executes locally in your browser's Web Worker, not on our server. It cannot inspect your normal PC files or Windows hardware. Avoid entering secrets. Heavy/infinite loops are stopped by a time limit.

Check your understanding.

Try answering each question before opening its reference answer. Explain the idea aloud or change an example to test it for yourself.

  1. Why use PureWindowsPath for Windows path examples in a browser that may run on another operating system?

    View answer

    PureWindowsPath interprets Windows path syntax without assuming that Python is currently running on Windows. It can split names, show parents and join components without contacting a filesystem. That makes it appropriate for cross-platform demonstrations inside a browser, even when the example shows a drive-letter path.

  2. What is the distinction between PureWindowsPath and Path when real files are involved?

    View answer

    PureWindowsPath only manipulates path representations; it cannot verify whether files exist. Path represents the host system’s real filesystem paths and can be used for operations such as checking existence, reading and writing when run in a suitable local environment. Browser security boundaries still prevent ordinary access to a visitor’s Windows files.

Where this leads.

Next, practise writing and checking small reusable string functions using ideas from the lessons so far.