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

H

happykid

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.
 
C

Chris Angelico

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.

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
 
T

Thijs Engels

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.


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
 
J

John Gordon

In said:
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.

What is the value of sys.argv[0]? You're supposed to pass a full
pathname to os.path.dirname, like so:

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:

Are you trying to obtain the full pathname of the program? That's an
entirely different question.
 
C

Chris Angelico

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
 
G

Gregory Ewing

Thijs said:
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.
 
W

WaffleSouffle

Thijs said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top