As long as you haven't changed directory since startup, you should be
able to use os.path.abspath(sys.argv[0]) to get the absolute path
name. It'll automatically fill in the path from the current directory
if it's not provided.
See: http://docs.python.org/py3k/library/os.path.html#os.path.abspath
ChrisA
I think this is what you are after:
import os.path
path = os.path.abspath(os.path.dirname(__file__))
argv[0] returns the name of the current file (string), but no path
information if I recall correct.
Thijs
What is the value of sys.argv[0]? You're supposed to pass a full
pathname to os.path.dirname, like so:
>>> import os
>>> os.path.dirname('/a/b/c/d/e.txt')
'/a/b/c/d'
>>>
If sys.argv[0] is just the program name, then it doesn't have a path,
which is why you get no results from os.path.dirname:
>>> import os
>>> os.path.dirname('foo.py')
''
>>>
Are you trying to obtain the full pathname of the program? That's an
entirely different question.
--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
It will give path information if you're invoking a script from another
directory. Under some circumstances it might happen to give an
absolute path for something in the current directory, but that can't
be assumed or relied upon.
ChrisA
> argv[0] returns the name of the current file (string), but no path
> information if I recall correct.
It's the path that was used to specify the script by whatever
launched it, so it could be either absolute or relative to
the current directory.
--
Greg
From the docs there are a couple of special cases: "If the command was
executed using the -c command line option to the interpreter, argv[0]
is set to the string '-c'. If no script name was passed to the Python
interpreter, argv[0] is the empty string."
So if you're running in a python interpreter compiled into an
executable which doesn't initialise argv, that's another case where
things won't go as expected.
That's probably not relevant in this case, but I've certainly been
bitten by it in the past.