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

Discussion in 'Python' started by Benjamin Han, Dec 27, 2003.

  1. Benjamin Han

    Benjamin Han Guest

    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!
    Benjamin Han, Dec 27, 2003
    #1
    1. Advertising

  2. Benjamin Han

    Benjamin Han Guest

    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!
    >
    Benjamin Han, Dec 28, 2003
    #2
    1. Advertising

  3. Benjamin Han

    Benjamin Han Guest

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

    >
    Benjamin Han, Dec 28, 2003
    #3
  4. 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
    Mark McEahern, Dec 28, 2003
    #4
  5. Benjamin Han

    Benjamin Han Guest

    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!
    Benjamin Han, Dec 28, 2003
    #5
  6. Benjamin Han

    Lee Harr Guest

    > #!/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.
    Lee Harr, Dec 28, 2003
    #6
  7. 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
    Wolfgang Lipp, Dec 29, 2003
    #7
  8. 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 |
    | | Compiler Manufacturer |
    Hartmut Goebel, Jan 3, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gmax2006
    Replies:
    7
    Views:
    595
    John Machin
    Aug 31, 2006
  2. Replies:
    4
    Views:
    332
    Keith Thompson
    Jun 21, 2007
  3. kj
    Replies:
    7
    Views:
    357
    Mike Driscoll
    Jun 9, 2008
  4. dude
    Replies:
    5
    Views:
    1,704
    Jussi Piitulainen
    Mar 17, 2011
  5. sm
    Replies:
    3
    Views:
    110
    Tad McClellan
    Jan 26, 2006
Loading...

Share This Page