How to get path of a .py script

Bernhard Herzog bh at intevation.de
Wed Dec 11 14:05:12 EST 2002


P_spam_ at draigBrady.com writes:

> P_spam_ at draigBrady.com wrote:
> > Devrim Erdem wrote:
> >
> >> Hi,
> >>
> >> On linux I have a script runSomething.py, the script is sometimes
> >> executed from a link on the desktop or from another dir.
> >>
> >> How can I get the path of the script file itself so that I can locate
> >> files which are relative to the script file ?
> > How about: os.dirname(os.path.realpath(sys.argv[0]))+"/myfile"
> 
> In fact you probably don't need to resolve symlinks etc.

Indeed. If the filename given to the Python interpreter is a symlink to
the actual script, Python puts the directory containing the actual
script into sys.path as the first item. So, sys.path[0] is usually
enough.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list