[Tutor] Opening files, finding their location

Christopher Arndt chris.arndt at web.de
Sat Oct 8 17:19:36 CEST 2005


Scott Oertel schrieb:
> I'm looking for an easy way to find the current directory location of my 
> program so I can include the config.ini file correctly without having to 
> pass command line args.

So, do you want to find out, where your script is living, or the directory from
which it was called (the "working directory")?

The former can be found out like this:

Assuming cron calls your script with the full path name (which should be the
case because it uses the shell to execute your script):

>>> import os, sys
>>> mydir = os.path.dirname(sys.argv[0])

When you want the current working directory:

>>> import os
>>> mydir = os.getcwd()

Then to get the path to your config file, either:

config_path = os.path.join(mydir, 'config.ini')

HTH, Chris


More information about the Tutor mailing list