Code of the Day
BeginnerAutomation Thinking

Paths and directories

The filesystem is a tree. Knowing absolute paths from relative ones — and using pathlib — keeps your scripts portable.

WorkflowBeginner6 min read
Recommended first
By the end of this lesson you will be able to:
  • Explain the difference between an absolute path and a relative path
  • Describe why hardcoded absolute paths break on other machines or accounts
  • Use pathlib.Path to construct portable, cross-platform paths

Before you can automate file work, you need a clear mental model of where files live. The filesystem is a tree. Every file is a leaf; every directory is a branch. The root of the tree is / on Unix-like systems (macOS, Linux) and C:\ (or similar) on Windows.

Absolute paths

An absolute path describes a location starting from the root of the tree:

/home/alice/projects/reports/sales.csv      # Linux / macOS
C:\Users\alice\projects\reports\sales.csv   # Windows

Absolute paths are unambiguous — they point to exactly one location regardless of where you are in the filesystem. The downside: they are machine-specific. A script that opens /home/alice/data.csv will crash for any other user, on any other machine, or even for Alice after she renames her home folder.

Relative paths

A relative path is measured from the current working directory — wherever the process was started from:

data/sales.csv          # the file at ./data/sales.csv
../shared/config.json   # one directory up, then into shared/

Relative paths are portable within a project, but fragile if the script is run from a different directory than expected.

Why hardcoded paths break

Here is the most common beginner mistake in automation scripts:

with open("/Users/alice/myproject/data.csv") as f:
    ...

This works exactly once, for one person, on one machine. Share the script with a colleague or run it in a CI environment and it breaks immediately. The fix is to build paths programmatically from known anchors — the project root, the script's own location, or an environment variable.

pathlib: the modern Python way

Python's module (built-in since Python 3.4) gives you path objects you can compose, inspect, and manipulate without string concatenation:

from pathlib import Path

# Build a path relative to this script's own location
here = Path(__file__).parent          # directory containing the script
data_file = here / "data" / "sales.csv"

print(data_file)          # /home/alice/myproject/data/sales.csv (absolute)
print(data_file.name)     # sales.csv
print(data_file.suffix)   # .csv
print(data_file.parent)   # /home/alice/myproject/data

The / operator joins path segments — no string slashes to manage, no os.path.join() to remember, and it handles the difference between \ (Windows) and / (Unix) automatically.

Path(__file__).parent is the standard anchor for scripts that ship with their data. For scripts that operate on external data, consider accepting the path as an argument rather than baking any path into the script at all.

Useful Path attributes

Attribute / methodReturns
.nameFilename with extension: "sales.csv"
.stemFilename without extension: "sales"
.suffixExtension: ".csv"
.parentContaining directory
.exists()True if the path exists on disk
.is_file()True if it's a regular file
.is_dir()True if it's a directory

Check your understanding

Knowledge check

  1. 1.
    Which of these is an absolute path?
  2. 2.
    Hardcoding an absolute path like /Users/alice/data.csv is fine as long as the script works on your machine.
  3. 3.
    What does Path("data") / "sales.csv" produce?

Where to go next

Next: automating file operations — using pathlib to find files matching a pattern and shutil to copy or move them in bulk.

Finished reading? Mark it complete to track your progress.

On this page