How to get path of a .py script

P_spam_ at draigBrady.com P_spam_ at draigBrady.com
Wed Dec 11 08:25:04 EST 2002


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.
so the following is more efficient, and will also
work with 1.5.2 at least:

   os.path.dirname(os.path.abspath(sys.argv[0]))

Pádraig.




More information about the Python-list mailing list