How to open my files in data_folder with pandas using relative path?

Luis Ramon Ramirez Rodriguez picture Luis Ramon Ramirez Rodriguez · Feb 13, 2016 · Viewed 166k times · Source

I'm working with pandas and need to read some csv files, the structure is something like this:

folder/folder2/scripts_folder/script.py

folder/folder2/data_folder/data.csv

How can I open the data.csv file from the script in scripts_folder?

I've tried this:

absolute_path = os.path.abspath(os.path.dirname('data.csv'))

pandas.read_csv(absolute_path + '/data.csv')

I get this error:

File folder/folder2/data_folder/data.csv does not exist

Answer

SPKoder picture SPKoder · Feb 13, 2016

Try

import pandas as pd
pd.read_csv("../data_folder/data.csv")