The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Run from directory?
Discuss Run from directory? in the Python Programming forum on Dev Shed. Run from directory? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 10th, 2003, 09:00 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Posts: 67
Time spent in forums: 41 m
Reputation Power: 10
|
|
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!
|

December 10th, 2003, 10:19 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
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.
|

December 12th, 2003, 10:09 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Posts: 67
Time spent in forums: 41 m
Reputation Power: 10
|
|
|
Thanks, that works great.
|

December 12th, 2003, 10:41 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

December 12th, 2003, 02:56 PM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
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.
|

December 12th, 2003, 03:48 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Ah now i get ya  thanx for the info Perc
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|