faqts : Computers : Programming : Languages : Python : Snippets : Stream Manipulation : Files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 1 of 1 people (100%) answered Yes

Entry

Directory of current file

Jul 5th, 2000 09:59
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 85, Fredrik Lundh


"""
Packages: files
"""
"""
> > 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]
print _thisDir