S
Sion Arrowsmith
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?
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?