finding pathname from within a module?

brueckd at tbye.com brueckd at tbye.com
Thu Sep 13 14:34:45 EDT 2001


On Thu, 13 Sep 2001, Alex Rice wrote:

> I know there must be a more pythonic way than this. My module needs to
> be able to read a datafile in the same directory the module is defined.
> What I came up with for creating the filename is this:
>
> pathEls = string.split(__file__, os.sep)[:-1]
> pathEls.append('my.data')
> fileName = string.join(pathEls, os.sep)
>
> Is there a way to do something like <thecurrentmodule>.__path__?

import imp, os
path = imp.find_module('yourmodule')[1]
filename = os.path.join(os.path.split(path)[0], 'datafilename')

HTH,
Dave






More information about the Python-list mailing list