File Paths
in Python
Navigate your file system with confidence: understand absolute paths, relative paths, and how Python handles them across operating systems.
What Is a File Path?
A file path is a string that tells your operating system (and Python) where to find a file or folder in the directory tree. Think of it like a street address: it provides step-by-step directions from one location to another.
The Directory Tree
Your file system is organized as a tree. The topmost folder is called the root. Every file lives somewhere inside this tree.
Why File Paths Matter
File path bugs are one of the most common errors for new Python programmers. Getting paths right means your code can find data files, save outputs, and run on other people's machines.
Absolute Paths
An absolute path starts from the root of the file system and specifies every directory on the way to the target. It works no matter what your current directory is.
Relative Paths
A relative path starts from your current working directory (CWD): the folder Python is "sitting in" when it runs your script.
Absolute vs Relative
| Absolute | Relative | |
|---|---|---|
| Starts from | Root ( / or C:\ ) | Current Working Directory |
| Example | /Users/alice/app.py | ./app.py |
| Portable? | ✕ Breaks on other machines | ✓ Works if project structure kept |
| Depends on CWD? | No | Yes |
| Best for | System config files, logs | Project files, shared repos |
The Current Working Directory
The current working directory (CWD) is the folder your Python process considers "here." Every relative path is resolved starting from this location.
Building Paths with os.path
Instead of manually writing slashes, use os.path.join() to build paths. It automatically picks the correct separator for the current OS.
Modern Paths with pathlib
Python 3.4+ introduced the pathlib module: an object-oriented way to handle file paths. It replaces most of os.path with cleaner syntax.
Step-by-Step: Finding the Right Path
Example 1: Reading a File
Your project structure looks like this, and you want to read config.json from main.py:
Example 2: Going Up with ..
Now you are running a script inside tests/ and need to access data.csv in the project root:
Example 3: Cross-Platform Safety
Your classmate uses Windows, you use macOS. How do you write paths that work on both systems?
Common Mistakes
Quick Reference Table
| Concept | Syntax / Tool | Purpose |
|---|---|---|
| Absolute path | /Users/alice/file.py | Full address from root |
| Relative path | ./data/file.csv | Address from CWD |
| Parent directory | .. | Go up one level |
| Build paths | Path() / "dir" / "file" | OS-safe path joining |
| Get CWD | Path.cwd() | Check where Python is running |
| Resolve path | path.resolve() | Convert relative to absolute |
| Check existence | path.exists() | Verify file is reachable |
Question 1 of 5
Which of the following is an absolute path?
Question 2 of 5
What does the .. symbol represent in a file path?
Question 3 of 5
Your CWD is /home/user/app. You use the relative path ../data/file.csv. What is the absolute path Python resolves?
Question 4 of 5
Why is os.path.join() or pathlib.Path preferred over manually typing path separators?
Question 5 of 5
Consider this project layout:
school_project/
src/
analysis.py
data/
grades.csv
README.md
You are editing analysis.py (inside src/) and need to read grades.csv (inside data/). Your CWD is school_project/src.
Which path correctly locates grades.csv from analysis.py?