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

Benjamin Kaplan benjamin.kaplan at case.edu
Mon Aug 1 13:56:31 EDT 2011


On Mon, Aug 1, 2011 at 1:38 PM, happykid <psyking841 at 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.
> --
>

sys.argv[0] is the name of the script you called. If you call "python
spam.py", sys.argv[0] will be spam.py. Not /full/path/spam.py. Just spam.py.
And the directory part of "spam.py" is "". You'll have to call
os.path.abspath(sys.argv[0]) to get the full path, then get dirname.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110801/3320a809/attachment-0001.html>


More information about the Python-list mailing list