Directory of current file

Fredrik Lundh fredrik at pythonware.com
Thu Apr 22 08:19:54 EDT 1999


Jeff Bauer <jbauer at rubic.com> wrote:
> David Ascher wrote:
> > Tip:  To find out the directory of the currently executing program, use:
> > 
> > import sys, os
> > if __name__ == '__main__':
> >     _thisDir = ''
> > else:
> >     _thisDir = os.path.split(sys.modules[__name__].__file__)[0]
> 
> David, what are the advantages over this?
> 
>   _thisDir = os.path.split(sys.argv[0])[0]

as far as I can tell, David's method works for everything
*except* the main module, while your method works for
the main module, but not for anything else...

how about a combination?

import sys, os
if __name__ == '__main__':
    _thisDir = sys.argv[0]
else:
    _thisDir = sys.modules[__name__].__file__
_thisDir = os.path.split(_thisDir)[0]

</F>





More information about the Python-list mailing list