finding pathname from within a module?

Alex Martelli aleax at aleax.it
Fri Sep 14 09:45:06 EDT 2001


"Alex Rice" <alex at integretechpub.com> wrote in message
news:3BA0E766.F7ABCAD9 at integretechpub.com...
> 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__?

    os.path.dirname(os.path.abspath(__file__))

should be just that.

Remember to use os.path.join to put that directory path together
with a file's basename, *NOT* string.join with os.sep, though!!!


Alex






More information about the Python-list mailing list