[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

argv[0] and __file__ inconsistency

Hai Vu

12/31/2007 10:32:00 PM

I currently use ActivePython 2.5.1. Consider the following code which
I saved as cmdline.py:
import sys
print sys.argv[0]
If I invoke this code as 'python cmdline.py', then the output is:
cmdline.py
If I invoke it as 'cmdline.py', then the output is:
C:\Users\hai\src\python\cmdline.py

The same happens for __file__. My question: do you have any
suggestions for a more consistent way to figure out the full path of
your script?
3 Answers

John Machin

12/31/2007 11:05:00 PM

0

On Jan 1, 9:31 am, Hai Vu <wuh...@gmail.com> wrote:
> I currently use ActivePython 2.5.1. Consider the following code which
> I saved as cmdline.py:
> import sys
> print sys.argv[0]
> If I invoke this code as 'python cmdline.py', then the output is:
> cmdline.py
> If I invoke it as 'cmdline.py', then the output is:
> C:\Users\hai\src\python\cmdline.py
>
> The same happens for __file__. My question: do you have any
> suggestions for a more consistent way to figure out the full path of
> your script?

use os.path.abspath

Benjamin A'Lee

12/31/2007 11:06:00 PM

0

On Mon, Dec 31, 2007 at 02:31:39PM -0800, Hai Vu wrote:
> I currently use ActivePython 2.5.1. Consider the following code which
> I saved as cmdline.py:
> import sys
> print sys.argv[0]
> If I invoke this code as 'python cmdline.py', then the output is:
> cmdline.py
> If I invoke it as 'cmdline.py', then the output is:
> C:\Users\hai\src\python\cmdline.py
>
> The same happens for __file__. My question: do you have any
> suggestions for a more consistent way to figure out the full path of
> your script?

How about::

from os.path import abspath
scriptname = abspath(sys.argv[0])

Hai Vu

12/31/2007 11:24:00 PM

0

> use os.path.abspath

Bingo! This is just what the doctor ordered. Thank you.
Hai