PYTHON FILE PATHS
A beginner's guide to understanding file paths in Python
WHAT ARE FILE PATHS?
A file path is the address that tells Python where to find a file on your computer. Just like you need a street address to find a house, Python needs a file path to open, read, or write files.
There are two types of file paths: absolute paths (complete addresses from the root of your computer) and relative paths (directions from where your script is currently located).
THE PROBLEM: "WHY DOES MY CODE WORK SOMETIMES BUT NOT OTHERS?"
You write this code and it works perfectly:
data = open('interviews.csv')
But then you run the same script from a different folder or share it with a colleague, and suddenly:
FileNotFoundError: [Errno 2] No such file or directory: 'interviews.csv'
This happens because Python is looking for the file in the wrong place. Understanding file paths prevents this frustration.
ABSOLUTE VS RELATIVE PATHS
ABSOLUTE PATH
Complete address from the root of your system. Always works regardless of where your script is run from.
/Users/sarah/projects/news-scraper/data/interviews.csv
C:\Users\Sarah\projects\news-scraper\data\interviews.csv
RELATIVE PATH
Directions from where your script is currently located. Portable but depends on working directory.
interviews.csv
data/interviews.csv
../output/analysis.xlsx
WORKING DIRECTORY: WHERE PYTHON THINKS IT IS
Your working directory is where Python "stands" when it runs. Relative paths are calculated from this location. If you're in the wrong directory, relative paths won't find your files.
import os
print(os.getcwd())
/Users/sarah/projects/news-scraper
python script.py from different folders, your working directory changes. This is why code works from one location but not another.
CROSS-PLATFORM ISSUES: WINDOWS VS MAC/LINUX
FORWARD SLASHES
Mac & Linux
data/interviews.csv
BACKSLASHES
Windows
data\interviews.csv
THE SOLUTION
Use pathlib (works everywhere)
Path('data') / 'interviews.csv'
WHY HARDCODED PATHS BREAK EVERYTHING
If you write: C:\Users\Sarah\projects\data.csv
Your teammate on Mac will see an error. Their computer doesn't have a C: drive or a user named Sarah!
NEVER hardcode absolute paths with usernames. Use relative paths or pathlib instead.
WHEN TO USE EACH APPROACH
- • Project files that stay together (scripts, data folders, outputs)
- • Code you'll share with teammates or publish on GitHub
- • Portable projects that run on different computers
- • When files are organized in a predictable structure
- • System-wide configuration files that don't move
- • One-off scripts that access files in fixed locations
- • Database files or logs stored in standard directories
- • When you're certain the path will never change
QUICK START GUIDE: USING PATHLIB
Python's pathlib module is the modern way to handle file paths. It works on all operating systems and eliminates slash direction headaches.
from pathlib import Path
# Open a file in the 'data' subfolder
data_file = Path('data') / 'interviews.csv'
with open(data_file) as f:
content = f.read()
from pathlib import Path
# Get the directory where THIS script lives
script_dir = Path(__file__).parent
# Build path relative to script location
data_file = script_dir / 'data' / 'interviews.csv'
output_file = script_dir / 'output' / 'analysis.xlsx'
from pathlib import Path
data_file = Path('scraped_data') / 'articles.json'
if data_file.exists():
print(f"Found file at: {data_file}")
else:
print(f"File not found! Looking in: {data_file.absolute()}")
from pathlib import Path
# Create 'output' folder if it doesn't exist
output_dir = Path('output')
output_dir.mkdir(exist_ok=True) # Won't error if already exists
# Now save your file
output_file = output_dir / 'report.csv'
output_file.write_text("Name,Count\nJohn,5")
from pathlib import Path
# Find all CSV files in 'data' folder
data_dir = Path('data')
csv_files = data_dir.glob('*.csv')
for csv_file in csv_files:
print(f"Processing: {csv_file.name}")
REAL-WORLD ANALOGY
ABSOLUTE PATH = GPS COORDINATES
"Meet me at 40.7580° N, 73.9855° W" (Times Square in New York). This works from anywhere in the world, but it's not portable - if the building moves, the coordinates are wrong. Also useless if someone else doesn't have access to that exact location.
RELATIVE PATH = DIRECTIONS FROM HERE
"Walk two blocks north, then turn left." This only makes sense from where you currently are. If you start somewhere else, the directions won't work. But if you package the directions with the starting point, anyone can follow them.
For Python projects: Use relative paths anchored to your script location with Path(__file__).parent. This is like saying "directions from where this script lives" - portable and reliable.
COMMON MISTAKES THAT BREAK FILE PATHS
/Users/sarah/project/data.csv won't work for your teammate Mike
FIX: Use relative paths instead
data\files\report.csv breaks on Mac/Linux
FIX: Use pathlib's / operator or forward slashes
Assuming the file is there without verifying leads to crashes
FIX: Use Path('file.csv').exists() before accessing
Running python src/script.py from project root vs running from src/ folder changes working directory
FIX: Use Path(__file__).parent to anchor paths to script location
my data file.csv can cause issues in some contexts
FIX: Use underscores or hyphens: my_data_file.csv
Writing open('data.csv') assumes file is in current working directory
FIX: Be explicit: Path(__file__).parent / 'data.csv'
GOLDEN RULES FOR FILE PATHS
Always import pathlib and use Path() objects. They handle OS differences automatically.
Use Path(__file__).parent to build paths relative to your script, not working directory.
No /Users/yourname/ or C:\Users\yourname\ in your code. Ever.
Use .exists() before accessing files. Fail gracefully with helpful error messages.
Keep data in data/, outputs in output/, scripts in src/. Consistent structure = fewer path issues.
If possible, test your code on a different computer or OS. Catches portability issues early.
MASTER PATTERN: Path(__file__).parent / 'data' / 'file.csv'
This pattern works everywhere, for everyone, every time. Commit it to muscle memory and 90% of your file path problems disappear.
QUICK REFERENCE CARD
Path('folder') / 'file.csv' // Join pathspath.exists() // Check if existspath.is_file() // Is it a file?path.is_dir() // Is it a folder?path.parent // Get parent folderpath.name // Get filenamepath.stem // Filename without extensionpath.suffix // File extension (.csv)my_project/├── main.py // Your main script├── data/│ ├── interviews.csv│ └── sources.json├── output/│ └── analysis.xlsx├── src/│ └── helpers.py└── README.md