top of page
  • Writer's pictureGio

The RIGHT WAY to Set Absolute Path in Python



Whenever you run your python scripts on your machine and want to retrieve files or directories, it is handy to find the file’s absolute path relative to the current directory automatically instead of hardcoding them.


Say that you are working on a python project with your peers called myproject and you need to load a dataset.csv file to train an ML model. Obviously, you want to allow your peers to load the same file in their machines so they can also seamlessly contribute to the same project. Here I show the wrong and the right way to set the absolute path for a file.


⛔️ The Wrong Way 👎

import pandas as pd

# please change it to your file path
dataset_path = "/Users/datamover/myproject/data/dataset.csv"

# read csv file
df = pd.read_csv(dataset_path)

This implementation has several disadvantages, which are:

  • anyone who want to work on the same project needs to adjust "dataset_path";

  • this implementation is not scalable as the number of files to load will grow;

  • it is not OS agnostic; in order words, you need to make sure to use the right slash, i.e., backslash (\) for windows and forward slash (/) for Linux (and Mac);

  • it shows a lack of coding skills by the person who wrote the code.


✅ The Right Way 👍

import os
import pandas as pd

# set the working directory in your IDE to the root 
DATA_DIR = "data" 
FILENAME = "dataset.csv"

# use path.join to retrieve automatically the absolute path
df = pd.read_csv(os.path.join(DATA_DIR, FILENAME))

This implementation fixes all problems highlighted in the previous case using python’s standard utility module os by utilising the function join(). Note that it is assumed that you set your IDE root/working directory to your project root, which is usually the outmost directory in your project.









66 views
bottom of page