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

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


Try

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


Pandas will start looking from where your current python file is located. Therefore you can move from your current directory to where your data is located with '..'For example:

pd.read_csv('../../../data_folder/data.csv')

Will go 3 levels up and then into a data_folder (assuming it's there)Or

pd.read_csv('data_folder/data.csv')

assuming your data_folder is in the same directory as your .py file.


For non-Windows users:

import pandas as pdimport osos.chdir("../data_folder")df = pd.read_csv("data.csv")

For Windows users:

import pandas as pddf = pd.read_csv(r"C:\data_folder\data.csv")

The prefix r in location above saves time when giving the location to the pandas Dataframe.