Thread: Path to self

    #1
  1. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    11

    Path to self


    How does one find the path to the currently running script? is it in a global variable or one of the modules?

    thanks in advance.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    >>> import sys
    >>> sys.path[0]
    'C:\\Documents and Settings\\Master\\Desktop\\Ryan'
    >>>

    Comments on this post

    • netytan agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    well you can import the os module and call for the current working dirctory, in python the current working directory is where the app was executed from. So if you try and execute it from say / but the script is in /bin/script.py the current working directory in python with still be / let us have an example :

    Code:
    sh-2.05b$ cd /
    sh-2.05b$ python
    Python 2.3.5 (#1, May 21 2005, 17:22:05)
    [GCC 3.3.5  (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.getcwd()
    '/'
    >>>
    as you can see it tells you where python was excuted at (sorry this is on linux)

    now for lets say i do this:
    Code:
    sh-2.05b$ cd /
    sh-2.05b$ /usr/bin/python
    Python 2.3.5 (#1, May 21 2005, 17:22:05)
    [GCC 3.3.5  (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-8.7.7.1)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.getcwd()
    '/'
    >>>
    Even though the os is excuting a file in /usr/bin/python, it still things the working directory is root('/'). Because there is where the os left off.

    so this method will only work if you are in the same directory when executing the script. I do not beleave there is any other way to do this. Hope this helps.
  6. #4
  7. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    11
    hey, thanks everyone for the replies.
    I tried that with the sys.path, but apparently it doesn't work the same on linux.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Hmm, seems to work for me, I am on python 2.3. I was wrong about what i said, sorry for my missleading. Thank you Yegg for correcting me. We most have posted at the same time, because when i went back to finish you had already posted before me, or i wouldn't have posted what i said.

    however in my test of it in linux i used this as my code:
    Code:
    #!/usr/bin/env python
    
    import sys
    
    print sys.path[0]
    then to run it i just did this:

    Code:
    # cd /
    # python /home/localuser/scripts/sysPath.py
    /home/localuser/scripts
    #
    so it does seem to work. Possibel you are doing something wrong?
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Yes I noticed that. Right after I posted I noticed that you also made a post. I was like, wow!, it was like 5-10 seconds right after my post I'm guessing.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    What you're actually looking for sys.argv[0]; this always contains the absolute path to your program, while sys.path[0] will contain the absolute path to your program without the files name. Here's an example that should explain this .

    Code:
    #!/usr/bin/env python
    
    import os, sys
    
    print os.getcwd()
    print sys.path[0]
    print sys.argv[0]
    Code:
    modem-402:~/Desktop Mark$ cd /
    modem-402:/ Mark$ python2.4 ~/Desktop/cwd.py
    /
    /Users/Mark/Desktop
    /Users/Mark/Desktop/cwd.py
    modem-402:/ Mark$ 
    modem-402:/ Mark$ cd /Users/
    modem-402:/Users Mark$ python2.4 ~/Desktop/cwd.py 
    /Users
    /Users/Mark/Desktop
    /Users/Mark/Desktop/cwd.py
    Just for fun, try moving the script around and changing your working directory to see how it effects the script .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    ahh, did not know that either. Thanks for the help.
  16. #9
  17. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Here is a slightly more complex way from DevCoach to do the same thing for objects in imported modules and which also works if you are running the application through a desktop short cut or as a compiled zip:
    Module location

    Code:
        def path(self):
            path = os.path.dirname( sys.modules[self.__class__.__module__].__file__)
            if 'library.zip' in path:
                path = os.path.dirname(path)
            return path
    grim
  18. #10
  19. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    11
    thanks to everyone who replied.

    I tried the sys.argv thing in interactive python before I posted, but now it makes sense that argv is empty in interactive but not for scripts.

    thanks.

IMN logo majestic logo threadwatch logo seochat tools logo