Home > other >  Python add path of data directory
Python add path of data directory

Time:12-17

I want to add a path to my data directory in python, so that I can read/write files from that directory without including the path to it all the time.

For example I have my working directory at /user/working where I am currently working in the file /user/working/foo.py. I also have all of my data in the directory /user/data where I want to excess the file /user/data/important_data.csv.

In foo.py, I could now just read the csv with pandas using

import pandas as pd
df = pd.read_csv('../data/important_data.csv')

which totally works. I just want to know if there is a way to include /user/data as a main path for the file so I can just read the file with

import pandas as pd
df = pd.read_csv('important_data.csv')

The only idea I had was adding the path via sys.path.append('/user/data'), which didnt work (I guess it only works for importing modules).

Is anyone able to provide any ideas if this is possible?

PS: My real problem is of course more complex, but this minimal example should be enough to handle my problem.

CodePudding user response:

It looks like you can use os.chdir for this purpose.

import os

os.chdir('/user/data')

See https://note.nkmk.me/en/python-os-getcwd-chdir/ for more details.

CodePudding user response:

If you are keeping everything in /user/data, why not use f-strings to make this easy? You could assign the directory to a variable in a cofig and then use it in the string like so:

In a config somewhere:

data_path = "/user/data"

Reading later...

df = pd.read_csv(f"{data_path}/important_data.csv")

  • Related