Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Simple way to get the full path of a running script?

Reply
Thread Tools

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

 
 
Benjamin Han
Guest
Posts: n/a
 
      12-27-2003
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!
 
Reply With Quote
 
 
 
 
Benjamin Han
Guest
Posts: n/a
 
      12-28-2003
Duh - the way I described seems to be simple enough:

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


On Sat, 27 Dec 2003, Benjamin Han wrote:

> 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!
>

 
Reply With Quote
 
 
 
 
Benjamin Han
Guest
Posts: n/a
 
      12-28-2003
Should be:

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

On Sat, 27 Dec 2003, Benjamin Han wrote:

> Duh - the way I described seems to be simple enough:
>
> pathToScript=os.join(os.getcwd(),os.path.split(sys .argv[0])[0])
>
>
> On Sat, 27 Dec 2003, Benjamin Han wrote:
>
> > 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!
> >

>

 
Reply With Quote
 
Mark McEahern
Guest
Posts: n/a
 
      12-28-2003
On Sat, 2003-12-27 at 18:02, Benjamin Han wrote:
> 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


 
Reply With Quote
 
Benjamin Han
Guest
Posts: n/a
 
      12-28-2003
On Sat, 27 Dec 2003, Mark McEahern wrote:

> On Sat, 2003-12-27 at 18:02, Benjamin Han wrote:
> > 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!
 
Reply With Quote
 
Lee Harr
Guest
Posts: n/a
 
      12-28-2003
> #!/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.

 
Reply With Quote
 
Wolfgang Lipp
Guest
Posts: n/a
 
      12-29-2003
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
 
Reply With Quote
 
Hartmut Goebel
Guest
Posts: n/a
 
      01-03-2004
Hi,

Wolfgang Lipp schrieb:

> 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 |
| http://www.velocityreviews.com/forums/(E-Mail Removed) | Compiler Manufacturer |

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.path.walk() to get full path of all files dude Python 5 03-17-2011 09:09 AM
How can I retrieve the full path of running exe somank.sharma@gmail.com C Programming 4 06-21-2007 09:07 AM
sys.argv[0] doesn't always contain the full path of running script. gmax2006 Python 7 08-31-2006 09:12 PM
Request.FilePath gets full path...built in way to grab just the filename? Darrel ASP .Net 3 05-17-2006 01:39 AM
How can I get the full path for the running script (like $0) sm Perl Misc 3 01-26-2006 12:51 PM



Advertisments