finding pathname from within a module?

Gordon McMillan gmcm at
Fri Sep 14 00:43:00 CEST 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('')
> fileName = string.join(pathEls, os.sep)
> Is there a way to do something like <thecurrentmodule>.__path__?

I'd do it as
  os.path.join(os.path.dirname(__file__), '')

BTW, a package (or, the __init__ module that defines a package)
has a __path__ attribute, but it's a list of paths (like sys.path).

- Gordon

More information about the Python-list mailing list