Paths and Folders

Find files reliably across operating systems

Beginner 10 min

In this lesson

A path is the address that tells Python where a file or folder lives, so it can find it before opening it. Getting paths right — in a way that works on Windows, macOS, and Linux, using pathlib — is a small skill that saves big headaches.

Explain it like I’m 5

A path is an address for a file. Some addresses are full (‘123 Main St, London’) and some are relative to where you’re standing (‘two doors down’). Python needs the right kind of address to find your file.

Absolute vs relative paths

An absolute path gives the full location from the root of the drive (/Users/ada/notes.txt or C:\Users\Ada\notes.txt). A relative path is relative to the current working directory — where your program is running from (data/notes.txt). Relative paths are shorter but depend on where you launch the script.

The working directory

The working directory is the folder Python considers ‘here’. A relative path like "notes.txt" is resolved against it, which is why the same script can find a file when run from one folder but not another. When a file ‘isn’t found’ but you’re sure it exists, the working directory is the usual culprit.

Example
from pathlib import Path

print("Running from:", Path.cwd())
print("Looking for:", Path("notes.txt").resolve())
Output
Running from: /home/ada/project
Looking for: /home/ada/project/notes.txt
Where 'here' is, and where a relative name resolves to. (Run locally.)

Why not to build paths with string concatenation

Gluing paths with + and "/" breaks across systems: Windows uses backslashes, others use forward slashes, and you get double or missing separators. The fix is the standard-library pathlib module, which handles separators correctly everywhere.

pathlib: the modern way

from pathlib import Path. Build paths with the / operator: Path("data") / "notes.txt". Check existence with .exists(), get the filename with .name, and list a folder with Path("data").iterdir(). pathlib makes paths readable and portable.

Example
from pathlib import Path

folder = Path("data")
file_path = folder / "report.txt"

print(file_path)
print("Name:", file_path.name)
print("Parent:", file_path.parent)
print("Exists?", file_path.exists())
Output
data/report.txt
Name: report.txt
Parent: data
Exists? False
Building and inspecting a path with pathlib. (Run locally.)

Creating folders and finding files

pathlib does far more than join paths. Path("data").mkdir(exist_ok=True) creates a folder, with the flag meaning ‘don’t error if it already exists’. Path(".").glob("*.txt") finds every file matching a pattern — the tidy way to ‘process all the CSVs in this folder’. And Path("notes.txt").read_text() reads a small file in a single line, a shortcut over the open() pattern.

Together these turn pathlib into a one-stop tool for working with the filesystem.

Example
from pathlib import Path

Path("reports").mkdir(exist_ok=True)
(Path("reports") / "jan.txt").write_text("January\n")
(Path("reports") / "feb.txt").write_text("February\n")

for f in sorted(Path("reports").glob("*.txt")):
    print(f.name)
Output
feb.txt
jan.txt
Create a folder, write files, then list matches. (Run locally.)

Common mistake: Hardcoding paths with the wrong separator

Why it happens:

A path written with backslashes (or forward slashes) for one OS fails on another, and backslashes also start escape sequences in strings.

How to fix it:

Use pathlib.Path and the / operator so Python inserts the correct separator for the current system.

Common mistake: Assuming the working directory is the script's folder

Why it happens:

The working directory is where you ran the program from, not necessarily where the script file lives.

How to fix it:

For files next to your script, build paths from Path(__file__).parent. To see the current directory, print Path.cwd().

What's the difference between an absolute and a relative path?

What's the recommended way to join path parts in modern Python?

Mini exercise (medium)

Using pathlib, build a path to logs/today.txt inside the current folder, print whether it exists, and print just its file name.

pathlib works here too. Build the path, then print whether it exists and just its file name. (It won’t exist in this sandbox — that’s fine.)

from pathlib import Path

p = Path(".")    # TODO: build  logs/today.txt
print(p.exists())
print(p.name)

What to learn next

You learned the difference between absolute and relative paths, how the working directory affects them, why gluing paths together with string concatenation breaks, and how pathlib joins paths and checks for files cleanly. You built the path logs/today.txt with pathlib and inspected its file name.

With the practical file skills covered, the next lessons sharpen how you write Python itself. List Comprehensions shows how to build and filter a list in one readable line instead of a multi-line loop. When you’re ready, continue to the next lesson.