Paths and Folders
Find files reliably across operating systems
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.
from pathlib import Path
print("Running from:", Path.cwd())
print("Looking for:", Path("notes.txt").resolve())
Running from: /home/ada/project Looking for: /home/ada/project/notes.txt
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.
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())
data/report.txt Name: report.txt Parent: data Exists? False
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.
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)
feb.txt jan.txt
Common mistake: Hardcoding paths with the wrong separator
A path written with backslashes (or forward slashes) for one OS fails on another, and backslashes also start escape sequences in strings.
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
The working directory is where you ran the program from, not necessarily where the script file lives.
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?
An absolute path is complete from the drive root; a relative path depends on the current working directory.
What's the recommended way to join path parts in modern Python?
pathlib joins parts portably; string concatenation breaks across operating systems.
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)
Path("logs") / "today.txt", then use .exists() and .name.
from pathlib import Path
p = Path("logs") / "today.txt"
print(p.exists())
print(p.name)
assert p.name == "today.txt", "the file name should be today.txt"
assert p.parent.name == "logs", "the file should sit in the logs folder"
print("✓ Path built!")