|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
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
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
Thanks, that works great.
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
||||
|
||||
|
Ah now i get ya
thanx for the info PercMark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Run from directory? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|