Finding the local directory of a file in the python path

are Dogue are.dogue at gmail.com
Tue May 17 21:44:41 EDT 2011


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



More information about the Python-list mailing list