Hello, sorry to post that here, but I thought somebody might know that.
When a "program.py" is launched from whatever directory, I would like to determine the full path of "program.py". I could not solve that so far, because os.getcwd() will return the directory where python has been launched, and sys.argv[0] the file name with the path either relative to the current, or the absolute path.
Is there a way to determine whether a path is absolute or relative in a portable way ?
Thanks for your comprehension.
peter
This should work:
import sys
import os
path = os.path.abspath(sys.argv[0])
dirname, basename = os.path.split(path)
···
On Friday 17 October 2003 12:15, Peter Wurmsdobler wrote:
Hello, sorry to post that here, but I thought somebody might know that.
When a "program.py" is launched from whatever directory, I would like to
determine the full path of "program.py". I could not solve that so far,
because os.getcwd() will return the directory where python has been
launched, and sys.argv[0] the file name with the path either relative to
the current, or the absolute path.
Is there a way to determine whether a path is absolute or relative in a
portable way ?
--
Frederic
Frederic,
Thanks a lot ! It workds. There is more than my python book mentions. dir(os.path) would have shown.
path = os.path.abspath(sys.argv[0])
dirname, basename = os.path.split(path)
peter
Peter Wurmsdobler wrote:
Frederic,
Thanks a lot ! It workds. There is more than my python book mentions.
dir(os.path) would have shown.>path = os.path.abspath(sys.argv[0])
>dirname, basename = os.path.split(path)
This should work to find the absolute path of the original call, but
finding the path of the location of the application robustly is a
notoriously difficult problem, at least on *nix, what with symbolic
links and all. The above may meet your needs, but I thought I'd warn
you.
If you do really know where the "real" app is living, there was a
discussion about this on this list a while back, and I'm pretty sure
some fairly robust code was posted. Check the archives.
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov