How to get full path to script?

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

    How to get full path to script?




    How can a script know its absolute path? (__file__ only gives the
    path it was used to invoke the script.)

    Basically, I'm looking for the Python equivalent of Perl's FindBin.

    The point of all this is to make the scripts location the reference
    point for the location of other files, as part of a self-contained
    distribution.

    TIA!

    Kynn

    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
  • Mark Tolonen

    #2
    Re: How to get full path to script?


    "kj" <socyl@987jk.co m.invalidwrote in message
    news:g2h3do$74t $1@reader2.pani x.com...
    >
    >
    >
    How can a script know its absolute path? (__file__ only gives the
    path it was used to invoke the script.)
    >
    Basically, I'm looking for the Python equivalent of Perl's FindBin.
    >
    The point of all this is to make the scripts location the reference
    point for the location of other files, as part of a self-contained
    distribution.
    >
    TIA!
    >
    Kynn
    >
    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.
    import os
    print os.path.abspath (__file__)

    -Mark

    Comment

    • kj

      #3
      Re: How to get full path to script?

      In <Bu2dnUy5AvRQtd HVnZ2dnUVZ_tqdn Z2d@comcast.com "Mark Tolonen" <M8R-yfto6h@mailinat or.comwrites:
      >import os
      >print os.path.abspath (__file__)
      Great. Thanks!

      Kynn

      --
      NOTE: In my address everything before the first period is backwards;
      and the last period, and everything after it, should be discarded.

      Comment

      • bukzor

        #4
        Re: How to get full path to script?

        On Jun 8, 12:52 pm, kj <so...@987jk.co m.invalidwrote:
        In <Bu2dnUy5AvRQtd HVnZ2dnUVZ_tqdn ...@comcast.com "Mark Tolonen" <M8R-yft....@mailina tor.comwrites:
        >
        import os
        print os.path.abspath (__file__)
        >
        Great.  Thanks!
        >
        Kynn
        >
        --
        NOTE: In my address everything before the first period is backwards;
        and the last period, and everything after it, should be discarded.
        Note that this doesn't quite work for symbolic links or compiled
        scripts, depending on your requirements.

        Comment

        • Mike Driscoll

          #5
          Re: How to get full path to script?

          On Mon, Jun 9, 2008 at 12:37 AM, bukzor <workitharder@g mail.comwrote:
          On Jun 8, 12:52 pm, kj <so...@987jk.co m.invalidwrote:
          >In <Bu2dnUy5AvRQtd HVnZ2dnUVZ_tqdn ...@comcast.com "Mark Tolonen" <M8R-yft...@mailinat or.comwrites:
          >>
          >import os
          >print os.path.abspath (__file__)
          >>
          >Great. Thanks!
          >>
          >Kynn
          >>
          >--
          >NOTE: In my address everything before the first period is backwards;
          >and the last period, and everything after it, should be discarded.
          >
          Note that this doesn't quite work for symbolic links or compiled
          scripts, depending on your requirements.
          --

          >
          For my compiled scripts, I usually use this variation:

          path = os.path.abspath (os.path.join(o s.path.dirname( sys.argv[0])))

          It's always worked for me.

          Mike

          Comment

          • kj

            #6
            Re: How to get full path to script?

            In <mailman.228.12 13022580.1044.p ython-list@python.org "Mike Driscoll" <kyosohma@gmail .comwrites:
            >For my compiled scripts, I usually use this variation:
            >path = os.path.abspath (os.path.join(o s.path.dirname( sys.argv[0])))
            Thanks. But why the os.path.join()? (BTW, I did read the docs
            before posting, but they make no sense to me; they say that
            os.path.join joins "one or more path components intelligently",
            but what does it mean to join *one* component?)

            Kynn

            --
            NOTE: In my address everything before the first period is backwards;
            and the last period, and everything after it, should be discarded.

            Comment

            • Mike Driscoll

              #7
              Re: How to get full path to script?

              On Mon, Jun 9, 2008 at 11:07 AM, kj <socyl@987jk.co m.invalidwrote:
              In <mailman.228.12 13022580.1044.p ython-list@python.org "Mike Driscoll" <kyosohma@gmail .comwrites:
              >
              >>For my compiled scripts, I usually use this variation:
              >
              >>path = os.path.abspath (os.path.join(o s.path.dirname( sys.argv[0])))
              >
              Thanks. But why the os.path.join()? (BTW, I did read the docs
              before posting, but they make no sense to me; they say that
              os.path.join joins "one or more path components intelligently",
              but what does it mean to join *one* component?)
              >
              Kynn
              >
              --
              NOTE: In my address everything before the first period is backwards;
              and the last period, and everything after it, should be discarded.
              --

              >
              The idea of the join method is to create the path in an OS agnostic
              fashion. Linux uses forward slashes and Windows uses backward slashes
              to join the parts. The join method does this for you so you don't have
              to.

              I think in this case, if I had my program installed to

              C:\Program Files\MyProgram

              It would put the slashes in correctly for Windows. However, there are
              ways to get the default program directory in Linux and then have the
              os.path.join create the path correctly there too. That's the idea
              anyway. Hopefully that isn't more confusing than what you read.

              Mike

              Comment

              • Sebastian \lunar\ Wiesner

                #8
                Re: How to get full path to script?

                Mike Driscoll <kyosohma@gmail .comat Montag 09 Juni 2008 18:20:
                On Mon, Jun 9, 2008 at 11:07 AM, kj <socyl@987jk.co m.invalidwrote:
                >In <mailman.228.12 13022580.1044.p ython-list@python.org "Mike Driscoll"
                ><kyosohma@gmai l.comwrites:
                >>
                >>>For my compiled scripts, I usually use this variation:
                >>
                >>>path = os.path.abspath (os.path.join(o s.path.dirname( sys.argv[0])))
                >>
                >Thanks. But why the os.path.join()? (BTW, I did read the docs
                >before posting, but they make no sense to me; they say that
                >os.path.join joins "one or more path components intelligently",
                >but what does it mean to join *one* component?)
                >>
                >Kynn
                >>
                >--
                >NOTE: In my address everything before the first period is backwards;
                >and the last period, and everything after it, should be discarded.
                >--
                >http://mail.python.org/mailman/listinfo/python-list
                >>
                >
                The idea of the join method is to create the path in an OS agnostic
                fashion. Linux uses forward slashes and Windows uses backward slashes
                to join the parts. The join method does this for you so you don't have
                to.
                I guess, you didn't get his point. He seems to be aware that os.path.join
                creates a path from _multiple_ strings by joining them with the correct
                separator used by the underlying platform.

                But he was asking why one would invoke os.path.join on a _single_ string, as
                you did in your example. I'm wondering about this, too. It doesn't make
                sense to me. os.path.join doesn't convert existing separators to the
                platform-specific ones. And even if it would, sys.argv[0] already contains
                a correct path, so there is nothing that needs conversion. So why use it
                with a _single_ argument? I'd appreciate an example, illustrating the use
                of this ;)


                --
                Freedom is always the freedom of dissenters.
                (Rosa Luxemburg)

                Comment

                • Mike Driscoll

                  #9
                  Re: How to get full path to script?

                  On Mon, Jun 9, 2008 at 12:42 PM, Sebastian lunar Wiesner
                  <basti.wiesner@ gmx.netwrote:
                  Mike Driscoll <kyosohma@gmail .comat Montag 09 Juni 2008 18:20:
                  >
                  >On Mon, Jun 9, 2008 at 11:07 AM, kj <socyl@987jk.co m.invalidwrote:
                  >>In <mailman.228.12 13022580.1044.p ython-list@python.org "Mike Driscoll"
                  >><kyosohma@gma il.comwrites:
                  >>>
                  >>>>For my compiled scripts, I usually use this variation:
                  >>>
                  >>>>path = os.path.abspath (os.path.join(o s.path.dirname( sys.argv[0])))
                  >>>
                  >>Thanks. But why the os.path.join()? (BTW, I did read the docs
                  >>before posting, but they make no sense to me; they say that
                  >>os.path.joi n joins "one or more path components intelligently",
                  >>but what does it mean to join *one* component?)
                  >>>
                  >>Kynn
                  >>>
                  >>--
                  >>NOTE: In my address everything before the first period is backwards;
                  >>and the last period, and everything after it, should be discarded.
                  >>--
                  >>http://mail.python.org/mailman/listinfo/python-list
                  >>>
                  >>
                  >The idea of the join method is to create the path in an OS agnostic
                  >fashion. Linux uses forward slashes and Windows uses backward slashes
                  >to join the parts. The join method does this for you so you don't have
                  >to.
                  >
                  I guess, you didn't get his point. He seems to be aware that os.path.join
                  creates a path from _multiple_ strings by joining them with the correct
                  separator used by the underlying platform.
                  >
                  But he was asking why one would invoke os.path.join on a _single_ string, as
                  you did in your example. I'm wondering about this, too. It doesn't make
                  sense to me. os.path.join doesn't convert existing separators to the
                  platform-specific ones. And even if it would, sys.argv[0] already contains
                  a correct path, so there is nothing that needs conversion. So why use it
                  with a _single_ argument? I'd appreciate an example, illustrating the use
                  of this ;)
                  >
                  >
                  --
                  Freedom is always the freedom of dissenters.
                  (Rosa Luxemburg)
                  --

                  >
                  Okay, basically the answer is that I'm kind of stupid. Months ago, the
                  users on the wxPython group were discussing this issue and one of them
                  posted that snippet of code to show how they worked around the issue.
                  I thought I'd try it and it worked great, although I couldn't really
                  follow what was happening at the time.

                  Looking at it now, there doesn't appear to be any reason for the
                  os.path.join part. I tried running one of my simple scripts with and
                  without it and they return the same string.

                  I apologize for propagating erroneous code.

                  Mike

                  Comment

                  Working...