Help with embedding fully qualified script name

Robert Brewer fumanchu at amor.org
Mon Feb 14 12:38:41 EST 2005


Kamilche wrote:
> To avoid pathname headaches, I've taken to including the following 3
> lines at the top of every script that will be double-clicked:
> 
> import os, sys
> pathname, scriptname = os.path.split(sys.argv[0])
> pathname = os.path.abspath(pathname)
> os.chdir(pathname)

Putting the sys.argv call in a main block would make it more
future-proof (for the day when your script becomes a library ;):

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

# application code goes here

if __name__ == '__main__':
    import sys
    pathname = os.path.normpath(os.path.dirname(sys.argv[0]))


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list