Python add path of data directory

Question:

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.

Asked By: JD.

||

Answers:

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 config 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")

Answered By: leonious

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.

Answered By: Yash
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.