Sion Arrowsmith wrote: > I have a module which needs to know what directory it's in, and to > refer to files in a sibling directory, something like App/src/foo.py > wants to read App/data/conf.xml . But I have no idea in what context > foo.py is going to be run -- it could be being run as a script, it > could be being imported as a module by another script from anywhere in > the directory structure, it's even possible someone will have called > execfile on it. The following works for everything I've tried: > > thisdir = os.path.dirname(os.path.normpath(__file__)) > siblingdir = os.path.normpath(os.path.join(testdir, os.path.pardir, > "sibling")) > > However, a colleague expressed disgust at this code, but not really > being a Python programmer had no better suggestions. Is there a neater > way of getting what I want?
If you plan to have your code executed out of a zip or an egg, you may have problems because __file__ is not what you want, at least with eggs and Python 2.[34]. There was a brief thread on this topic on the distutils SIG mailing list within the last few days. HTH, John -- http://mail.python.org/mailman/listinfo/python-list