By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

argv[0] and __file__ inconsistency

P: n/a
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?
Dec 31 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jan 1, 9:31 am, Hai Vu <wuh...@gmail.comwrote:
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
Dec 31 '07 #2

P: n/a
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])

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFHeXXuEUZDNrttL6ARAh+6AKDIuqZr76t94DnRWtULHX mZEY7oNgCgklEr
Nnzko5YaVkEa7Tlanz/0Ehk=
=b4SC
-----END PGP SIGNATURE-----

Jan 1 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.