finding dir of main .py file

Matt Nordhoff mnordhoff at mattnordhoff.com
Tue Dec 11 14:50:48 EST 2007


ron.longo wrote:
> Nope, maybe I'm not explaining myself well.
> 
> When I do os.getenv('HOME') I get back None.
> 
> According to the docs, 'HOME' is the user's home directory on some
> platforms.  Which is not what I want.
> 
> What I want is the directory in which an application's main .py file
> resides.  That is, when I type: python MyApp.py, I want to know in which
> directory does MyApp.py reside?

Shane is right.

>>> print __file__

>>> print modulename.__file__

Just call os.path.dirname() on __file__ to get the directory.
-- 



More information about the Python-list mailing list