Finding the local directory of a file in the python path


A

are Dogue

Hi there,

I have a problem that I can't seem to solve after quite a bit of
searching, probably because I can't find the right terms to search
for.

Basically, here's the situation. Let's say I have a file at ~/foo/bin/
foo.py that imports a script that at ~/bar/bin/bar.py. The imported
file needs to load something from '~/bar/bin/data/', but I cannot hard
code the full path. Instead, what I need is to have something in
bar.py like

dir = .... # Not sure what this line should be
datadir = dir + '/data/'

If I use sys.path[0] or os.getcwd(), then when I import the file from
~/foo/bin/, I get that dir is '~/foo/bin' rather than '~/bar/bin'

I hope this explanation makes sense. If you have any insights, I would
be most appreciative.

Thanks
Are
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top