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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Han

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

    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

    #2
    Re: Simple way to get the full path of a running script?

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

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


    On Sat, 27 Dec 2003, Benjamin Han wrote:
    [color=blue]
    > 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!
    >[/color]

    Comment

    • Benjamin Han

      #3
      Re: Simple way to get the full path of a running script?

      Should be:

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

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

      Comment

      • Mark McEahern

        #4
        Re: Simple way to get the full path of a running script?

        On Sat, 2003-12-27 at 18:02, Benjamin Han wrote:[color=blue]
        > Duh - the way I described seems to be simple enough:
        >
        > pathToScript=os .join(os.getcwd (),os.path.spli t(sys.argv[0])[0])[/color]

        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.abspat h(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


        Comment

        • Benjamin Han

          #5
          Re: Simple way to get the full path of a running script?

          On Sat, 27 Dec 2003, Mark McEahern wrote:
          [color=blue]
          > On Sat, 2003-12-27 at 18:02, Benjamin Han wrote:[color=green]
          > > Duh - the way I described seems to be simple enough:
          > >
          > > pathToScript=os .join(os.getcwd (),os.path.spli t(sys.argv[0])[0])[/color]
          >
          > me = sys.argv[0]
          > print "This is the absolute path: %s" % (os.path.abspat h(me),)[/color]

          Thank you - this is even better!

          Comment

          • Lee Harr

            #6
            Re: Simple way to get the full path of a running script?

            > #!/usr/bin/env python[color=blue]
            >
            > 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.abspat h(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
            >[/color]


            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.

            Comment

            • Wolfgang Lipp

              #7
              Re: Simple way to get the full path of a running script?

              i normally use the following:

              import inspect
              print inspect.getsour cefile( lambda:None )

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

              _wolf

              Comment

              • Hartmut Goebel

                #8
                Re: Simple way to get the full path of a running script?

                Hi,

                Wolfgang Lipp schrieb:
                [color=blue]
                > import inspect
                > print inspect.getsour cefile( lambda:None )[/color]

                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 |
                | h.goebel@crazy-compilers.com | Compiler Manufacturer |

                Comment

                Working...