Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

os.path.dirname(sys.argv[0]) always returns nothing

1,292 views
Skip to first unread message

happykid

unread,
Aug 1, 2011, 1:38:07 PM8/1/11
to
I want to use this function to get the directory path of the running
script, but it always returns empty string. Can anyone help me solve
this? Thank you.

Chris Angelico

unread,
Aug 1, 2011, 1:53:30 PM8/1/11
to pytho...@python.org

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

Thijs Engels

unread,
Aug 1, 2011, 1:55:11 PM8/1/11
to pytho...@python.org
On Mon, 01 Aug 2011 10:38 -0700, "happykid" <psyki...@gmail.com>
wrote:

> I want to use this function to get the directory path of the running
> script, but it always returns empty string. Can anyone help me solve
> this? Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list


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

John Gordon

unread,
Aug 1, 2011, 1:58:27 PM8/1/11
to

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"

Chris Angelico

unread,
Aug 1, 2011, 2:01:08 PM8/1/11
to pytho...@python.org
On Mon, Aug 1, 2011 at 6:55 PM, Thijs Engels <th...@buckazoids.com> wrote:
> argv[0] returns the name of the current file (string), but no path
> information if I recall correct.

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

Gregory Ewing

unread,
Aug 1, 2011, 7:57:11 PM8/1/11
to
Thijs Engels wrote:

> 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

WaffleSouffle

unread,
Aug 9, 2011, 9:07:23 AM8/9/11
to

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.

0 new messages