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

WaffleSouffle wafflesouffle at gmail.com
Tue Aug 9 15:07:23 CEST 2011


On Aug 2, 12:57 am, Gregory Ewing <greg.ew... at canterbury.ac.nz> wrote:
> 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

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



More information about the Python-list mailing list