get directory name of file being executed

Mark McEahern marklists at mceahern.com
Thu Feb 27 08:41:29 EST 2003


[David N. Welton]
> I'm doing this:
> 
> /usr/bin/python2 /some/script/in/a/dir/foo.py
> 
> I want to get "/some/script/in/a/dir/", not the cwd of where I
> launched the script from.
> 
> Is that possible?

#!/usr/bin/env python

import os
import sys

if __name__ == '__main__':
    this_script = sys.argv[0]
    print 'this_script: %s' % this_script
    print "this_script's dir: %s" % os.path.dirname(this_script)

** Output:

$ /cygdrive/c/temp/junk.py
this_script: /cygdrive/c/temp/junk.py
this_script's dir: /cygdrive/c/temp

etc.

Cheers,

// m
-






More information about the Python-list mailing list