1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

But I can only have one path as cwd right?
Yes. But one thing you might be able to do is set up a symbolic link to one of the folders inside the other folder; see: linuxhint.com/symlink-a-directory-in-linux (if you're on Mac/Linux)
That's the answer I was looking for. Using os.symlink('/user/data/', 'data'), I can achieve what I was looking for. Thank you! (Maybe add the os.symlink to your answer)
For completeness, also can be done in Windows: computerhope.com/mklink.htm
1

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

1 Comment

This works of course, but thats not exactly what I am looking for. I really want the second code block to work. But thanks for the creative input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.