How to use relative paths in Python
This guide shows you how to write a Python script whose file paths keep working when you move it between your own computer and LINCOMM (Linux Community Servers). Python makes this clean: you anchor paths to the script's own location, and then it runs correctly from anywhere. For the concept behind this, see Understanding relative paths.
Prerequisites
- A Python script and its data files, kept together in one project folder.
- Python 3, which is what LINCOMM provides.
Steps
-
Lay out your project so the data sits in a known place relative to the script:
myproject/ analysis.py data/ transactions.csv -
At the top of
analysis.py, anchor to the script's own folder usingpathlib. The built-in name__file__is the path to the running script, and.parentis the folder it lives in:from pathlib import Path here = Path(__file__).parentHEREnow points to the folder containinganalysis.py, on any computer. -
Build every data path from
HERE, then read as normal. The/operator joins path parts safely on every operating system:import pandas as pd data_file = HERE / "data" / "transactions.csv" df = pd.read_csv(data_file)To reach a file in the parent folder, use
HERE.parent, which is one level up. -
Run the script:
python analysis.py
Verify it worked
Copy the whole myproject folder to LINCOMM, then run the script from a different folder than the project itself:
cd /mnt/aae/users/jdoe
python myproject/analysis.pyIt should find transactions.csv and run without any path edits, even though you launched it from outside the project folder.
If something went wrong
FileNotFoundError: Check that the data folder was copied alongside the script, and that the names passed toPathmatch the real folder and file names exactly (Linux is case-sensitive).- Worked when run from inside the folder but not outside: You may be using a bare path like
"data/transactions.csv"instead of anchoring toHERE. Build the path fromHEREas shown in step 3.