Name of the Script

Paul Moore paul.moore at uk.origin-it.com
Mon Mar 5 16:40:08 CET 2001


On Mon, 5 Mar 2001 16:17:38 +0100 , Pearu Peterson <pearu at cens.ioc.ee>
wrote:
>--- a.py ---
>import os,sys
>print os.path.abspath(sys.argv[0])
>---
>might be what you want.

Close, but not quite...

----
C:\Data>python a.py
C:\Data\a.py
C:\a.py

C:\Data>type a.py
import os,sys
print os.path.abspath(sys.argv[0])
os.chdir("C:/")
print os.path.abspath(sys.argv[0])
----

OK, so you could say "don't do that". But if I'm looking at some code
deep in a big program, can I be *sure* that no-one has changed
directory on me?

Interestingly, although the __name__ symbol has the value __main__ in
a script being run as a "main program", the __file__ symbol doesn't
exist. That's a pity, as it would probably be what I wanted...

Paul.




More information about the Python-list mailing list