Module directory

Gilles Lenfant glenfant at nospam-e-pack.net
Fri Sep 8 08:45:14 EDT 2000


Alex,

Thanks for the pointer but:
import os.path
print os.path.dirname(__file__)
Returns a name error on __file __ that is an attribute of a module and does
not take the current module as default module when not specified.

As a workaround, I import a dummy module that's in the same directory as the
main python file:

import os.path, dummy
print os.path.dirname(dummy.__file__)

Not very clean but it works

Sincerely

Gilles

>
> A module has a __file__ attribute that gives the complete path
> to the file from which the module has been loaded.  Therefore:
>
>     os.path.dirname(__file__)
>
> when called from any function (with an import os.path in the
> module, of course:-) will give the directory-name from which
> the module that contains the function was loaded (this assumes
> the function's func_globals have not been tampered with, of
> course, but who ever does that?).
>
>
> Alex
>
>
>





More information about the Python-list mailing list