Simple way to get the full path of a running script?

B

Benjamin Han

I know I can do this by get sys.argv[0], tell if it's a full path, and if not,
somehow join the relative path with getcwd(). Just wondering if there's a
simpler way to do this. Thanks!
 
B

Benjamin Han

Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys.argv[0])[0])
 
B

Benjamin Han

Should be:

pathToScript=\
os.path.normpath(os.path.join(os.getcwd(),os.path.split(sys.argv[0])[0]))

Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys.argv[0])[0])


I know I can do this by get sys.argv[0], tell if it's a full path, and if not,
somehow join the relative path with getcwd(). Just wondering if there's a
simpler way to do this. Thanks!
 
M

Mark McEahern

Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys.argv[0])[0])

Or:

#!/usr/bin/env python

import sys
import os

me = sys.argv[0]
print "This is how you invoked me: %s" % (me,)
print "This is the absolute path: %s" % (os.path.abspath(me),)

Usage:

mark@dev /var/tmp/buffer
$ python ../junk.py
This is how you invoked me: ../junk.py
This is the absolute path: /var/tmp/junk.py

Cheers,

// m
 
B

Benjamin Han

Duh - the way I described seems to be simple enough:

pathToScript=os.join(os.getcwd(),os.path.split(sys.argv[0])[0])

me = sys.argv[0]
print "This is the absolute path: %s" % (os.path.abspath(me),)

Thank you - this is even better!
 
L

Lee Harr

#!/usr/bin/env python
import sys
import os

me = sys.argv[0]
print "This is how you invoked me: %s" % (me,)
print "This is the absolute path: %s" % (os.path.abspath(me),)

Usage:

mark@dev /var/tmp/buffer
$ python ../junk.py
This is how you invoked me: ../junk.py
This is the absolute path: /var/tmp/junk.py


Is this going to work well cross platform? How about from IDLE?

I was using sys.path[0] which I thought was working well, but
apparently fails when the script is run from IDLE on windows.
 
W

Wolfgang Lipp

i normally use the following:

import inspect
print inspect.getsourcefile( lambda:None )

the lambda function could be any object; the getsourcefile returns the
path to the file where that object was defined.

_wolf
 
H

Hartmut Goebel

Hi,

Wolfgang said:
import inspect
print inspect.getsourcefile( lambda:None )

Nice idea!

Enhancement: when using inspect.getfile() the snippet will work even for
module without sourcefile, too.

--
Regards
Hartmut Goebel

| Hartmut Goebel | We build the crazy compilers |
| (e-mail address removed) | Compiler Manufacturer |
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top