#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    67
    Rep Power
    11

    Question Run from directory?


    How would you determine what directory a program is being run from?
    Thanks.
    Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Unless the file is a symlink, sys.argv[0] should give the path to the script being run, and os.getcwd() will give the directory you called the script from.

    If you want to find out where the script being run truly is, perhaps this is the best way:
    Code:
    os.path.realpath(sys.argv[0])
    realpath() will return the canonical path of the argument, following symlinks. This way, you won't have to check if the file run is a symlink or not, realpath() will always return the canonical path.
    Last edited by percivall; December 10th, 2003 at 10:33 PM.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    67
    Rep Power
    11
    Thanks, that works great.
    Before you criticize someone, walk a mile in their shoes, that way when you do criticize them, you're a mile away and you have their shoes!
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Since realpath() is only available on *nix its hard for me to play around with and being a total newb when it comes to *nix i have a few questions . A symlink is like a shortcut on Windows? Or does Windows not have an equivinet to symlink?

    If the first is True then i should mention that even when a program is run from a shortcut sys.argv[0] always returns the absolute path to the program, which is not effected by the shortcut... i don't know about *nix though obviously so sorry if this sounds silly

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

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

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    In unix, sys.argv[0] will give the path to the file/object you used as argument for execution, whether that is the original file or the symlink. If your file is in your path, this just means the path information is given implicitly.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ah now i get ya thanx for the info Perc

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


IMN logo majestic logo threadwatch logo seochat tools logo