1

I stetted up a Spyder project where I have codes located in a specific folder and different datas in different folders. Basically, I would like to read those files using the relative path or a simple approach. Let's take the project tree below as example:

Project Tree

I am trying to read "dummy_csv.csv" using "dummy_code.py".

What I am currently doing is this:

import pandas as pd
filepath= "../../../../../dummy_folder02/untitled folder/untitled folder/untitled
folder/dummy_data/dummy_csv.csv"
pd.read_csv(filepath)

I wonder if there is a more elegant/cleaner way of doing this...

malibu_jj
  • 13
  • 4

1 Answers1

1

You can include the root dir of your data in the system path variable, and then use just the relavtive path:

import sys
sys.path.append(<absolute path to root data dir>)

filepath = "<relative path to csv file, in relation to the absolute path added to sys.path>"

for example:

sys.path.append("C:/my_datasets/dummy_folder02")
filepath = "untitled folder/untitled folder/untitled folder/dummy_data/dummy_csv.csv"
alivne
  • 458
  • 2
  • 10