opening a file using a relative path from a subclass in a package
modelnine at bit-bukket.org
Wed Dec 7 22:05:07 CET 2005
spike grobstein wrote:
> I'd like the packages to define a file path for supporting files
> (graphics, etc) that are stored inside the package. The problem is that
> the superclass's definition (stored elsewhere) has all of the code for
> actually opening the files, so when I use the
> os.path.dirname(os.path.abspath(__file__)) trick, it's finding the
> superclass, not the package's path.
if you have an instance object (self) at hand, otherwise
if you have a class object (selfcls) at hand.
Thing being why your approach doesn't work is that __file__ references a
global which is always resolved in the namespace the function is defined
in, so the source file it's defined in. You can get at the module object
(the global namespace) of a class object by using the sys.modules trick.
Be aware that this is pretty... non-standard stuff, to say it mildly... ;-)
More information about the Python-list