01 / 20

File Paths
in Python

Navigate your file system with confidence: understand absolute paths, relative paths, and how Python handles them across operating systems.

Python

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.

/ or \
Separator characters that divide folder names in a path
Directory
Another word for "folder": a container that holds files

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.

/ Users var tmp alice bob projects docs app.py

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.

FileNotFoundError
The most common result of a wrong path: Python simply cannot locate the file you asked for.
Portability
Hardcoded paths break when your code moves to a different computer or operating system.
Collaboration
Using relative paths lets teammates clone your project and run it without editing paths.

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.

# macOS / Linux "/Users/alice/projects/app.py" # Windows "C:\\Users\\alice\\projects\\app.py"

Relative Paths

A relative path starts from your current working directory (CWD): the folder Python is "sitting in" when it runs your script.

# If CWD is /Users/alice/projects "app.py" # same folder "data/input.csv" # subfolder 'data' "../docs/readme.md" # go up, then into 'docs'
. (single dot)
Means "the current directory." Often optional but makes intent clear.
.. (double dot)
Means "the parent directory." Go up one level in the tree.

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.

import os # Check the current working directory print(os.getcwd()) # Output: /Users/alice/projects # Change it os.chdir("/Users/alice/docs") print(os.getcwd()) # Output: /Users/alice/docs

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.

import os # Let Python choose the separator path = os.path.join("data", "raw", "input.csv") print(path) # Linux/macOS: data/raw/input.csv # Windows: data\raw\input.csv
os.path.join()
Concatenates parts with the OS-specific separator
os.path.abspath()
Converts a relative path to its absolute equivalent

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.

from pathlib import Path # Build a path using the / operator p = Path("data") / "raw" / "input.csv" print(p) # data/raw/input.csv print(p.resolve()) # /Users/alice/projects/data/raw/input.csv print(p.exists()) # True or False

Step-by-Step: Finding the Right Path

1
Identify the target file and figure out which folder it lives in.
2
Check your CWD with os.getcwd() or Path.cwd() to know where Python is running from.
3
Choose relative or absolute: if the file is inside your project, use a relative path. Otherwise, use absolute.
4
Build the path using pathlib.Path() or os.path.join(): never hardcode slashes.
5
Verify it exists with Path.exists() or os.path.exists() before reading or writing.

Example 1: Reading a File

Your project structure looks like this, and you want to read config.json from main.py:

my_project/ main.py config/ config.json
from pathlib import Path config_path = Path("config") / "config.json" with open(config_path) as f: data = f.read()

Example 2: Going Up with ..

Now you are running a script inside tests/ and need to access data.csv in the project root:

my_project/ data.csv src/ tests/ test_app.py .. (go up)
from pathlib import Path # From tests/test_app.py, go up one level data_path = Path("..") / "data.csv" with open(data_path) as f: lines = f.readlines()

Example 3: Cross-Platform Safety

Your classmate uses Windows, you use macOS. How do you write paths that work on both systems?

# BAD: hardcoded separator path = "data/raw/input.csv" # fails on Windows! path = "data\\raw\\input.csv" # fails on macOS! # GOOD: let Python handle it from pathlib import Path path = Path("data") / "raw" / "input.csv" # ALSO GOOD: os.path.join import os path = os.path.join("data", "raw", "input.csv")

Common Mistakes

✕ Hardcoding Absolute Paths
Writing "/Users/alice/data.csv" works on your machine but breaks for everyone else.
✕ Assuming the CWD
Your script assumes CWD is the project root, but an IDE or cron job may set it differently.
✕ Using Wrong Separators
Typing \ on macOS or / in Windows string literals without escaping causes silent path errors.

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

Which path correctly locates grades.csv from analysis.py?