Finding a script's home directory?

Mark McEahern marklists at
Mon Jan 24 10:52:54 EST 2005

Gabriel Cooper wrote:

> In one of my python programs has a data file I need to load. My 
> solution was to say:
>        if os.path.exists(os.path.join(os.getcwd(), "config.xml")):
>            self.cfgfile = os.path.join(os.getcwd(), "config.xml")
> Which works fine... as long as you're *in* the script's home directory 
> when you run it (as in, run it as: ./ as opposed to 
> ./myApp/
> If I run it from an alternate directory the program looks for the 
> config.xml file in my current directory not the app's home directory. 
> So how do I get the script's home directory?

import os.path
dirname = os.path.dirname(__file__)

// m

More information about the Python-list mailing list