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:

YOUR SCRIPT
data = open('interviews.csv')

But then you run the same script from a different folder or share it with a colleague, and suddenly:

ERROR OUTPUT
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.

MAC / LINUX EXAMPLE
/Users/sarah/projects/news-scraper/data/interviews.csv
WINDOWS EXAMPLE
C:\Users\Sarah\projects\news-scraper\data\interviews.csv
Always works from anywhere
Unambiguous and explicit
Breaks when shared with others
Not portable across systems
RELATIVE PATH

Directions from where your script is currently located. Portable but depends on working directory.

SAME FOLDER
interviews.csv
SUBFOLDER
data/interviews.csv
PARENT FOLDER
../output/analysis.xlsx
Portable across systems
Works when sharing projects
Depends on working directory
Can break if run from wrong place

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.

CHECK YOUR CURRENT WORKING DIRECTORY
PYTHON CODE
import os print(os.getcwd())
# Shows where Python currently "is"
EXAMPLE OUTPUT
/Users/sarah/projects/news-scraper
CRITICAL: When you run 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

USE RELATIVE PATHS FOR:
  • 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
USE ABSOLUTE PATHS FOR:
  • 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
RULE OF THUMB: Default to relative paths for 95% of your work. Only use absolute paths when you have a specific reason.

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.

EXAMPLE 01 // BASIC FILE ACCESS
PYTHON CODE
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()
EXAMPLE 02 // RELATIVE TO SCRIPT LOCATION
ALWAYS WORKS REGARDLESS OF WHERE SCRIPT IS RUN FROM
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'
# __file__ is the path to the current script
EXAMPLE 03 // CHECKING IF FILE EXISTS
AVOID ERRORS BEFORE THEY HAPPEN
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()}")
EXAMPLE 04 // CREATING DIRECTORIES
SAFELY CREATE OUTPUT FOLDERS
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")
EXAMPLE 05 // LISTING FILES IN A DIRECTORY
PROCESS MULTIPLE FILES
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

HARDCODING YOUR USERNAME

/Users/sarah/project/data.csv won't work for your teammate Mike

FIX: Use relative paths instead

USING BACKSLASHES ON WINDOWS

data\files\report.csv breaks on Mac/Linux

FIX: Use pathlib's / operator or forward slashes

NOT CHECKING IF FILE EXISTS

Assuming the file is there without verifying leads to crashes

FIX: Use Path('file.csv').exists() before accessing

RUNNING FROM WRONG DIRECTORY

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

SPACES IN FILE NAMES

my data file.csv can cause issues in some contexts

FIX: Use underscores or hyphens: my_data_file.csv

ASSUMING CURRENT DIRECTORY

Writing open('data.csv') assumes file is in current working directory

FIX: Be explicit: Path(__file__).parent / 'data.csv'

GOLDEN RULES FOR FILE PATHS

01
USE PATHLIB, NOT STRINGS

Always import pathlib and use Path() objects. They handle OS differences automatically.

02
ANCHOR TO SCRIPT LOCATION

Use Path(__file__).parent to build paths relative to your script, not working directory.

03
NEVER HARDCODE USERNAMES

No /Users/yourname/ or C:\Users\yourname\ in your code. Ever.

04
CHECK EXISTENCE FIRST

Use .exists() before accessing files. Fail gracefully with helpful error messages.

05
ORGANIZE WITH FOLDERS

Keep data in data/, outputs in output/, scripts in src/. Consistent structure = fewer path issues.

06
TEST ON DIFFERENT MACHINES

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

COMMON PATHLIB OPERATIONS
Path('folder') / 'file.csv' // Join paths
path.exists() // Check if exists
path.is_file() // Is it a file?
path.is_dir() // Is it a folder?
path.parent // Get parent folder
path.name // Get filename
path.stem // Filename without extension
path.suffix // File extension (.csv)
TYPICAL PROJECT STRUCTURE
my_project/
├── main.py // Your main script
├── data/
│ ├── interviews.csv
│ └── sources.json
├── output/
│ └── analysis.xlsx
├── src/
│ └── helpers.py
└── README.md