pythonfilepath

Python add path of data directory


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.


Solution

  • 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.