argv[0] and __file__ inconsistency

Benjamin M. A'Lee bma+lists at subvert.org.uk
Mon Dec 31 18:06:22 EST 2007


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])
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20071231/ea22e983/attachment-0001.sig>


More information about the Python-list mailing list