简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM