argv[0] and __file__ inconsistency

H

Hai Vu

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?
 
J

John Machin

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
 
B

Benjamin M. A'Lee

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+6AKDIuqZr76t94DnRWtULHXmZEY7oNgCgklEr
Nnzko5YaVkEa7Tlanz/0Ehk=
=b4SC
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,294
Latest member
HollieYork

Latest Threads

Top