tags:

views:

155

answers:

5

I tried os.__file__ but that returns the name of the file where os resides.

+1  A: 

sys.argv[0] should give you the name of the script.

Taylor Leese
+7  A: 
>> import os
>> import sys
>> print sys.argv[0]

or if you just want the script and not the full path

>>
>> print os.path.basename(sys.argv[0])
Michael Foukarakis
+11  A: 

thisFile = __file__

it's magic! ; )

Chazadanga
A: 

sys.path[0]

returns the path of the script that launched the python interpreter.

If you can this script directly, it will return the path of the script.

If the script however, was imported from another script, it will return the path of that script.

I had this question myself once and wrote it down at

http://arcoleo.org/dsawiki/Wiki.jsp?page=Python%20Path%20Issues

Dave
+2  A: 

It depends on what you mean by "a running python script".

__file__ will give you the name of the currently executing file. If that's a module, you'll get where it was imported from e.g. blahblah.pyc

sys.argv[0] will give you the name of the script that is being run, even if called from a module that that script imported.

Please do look up the answers to the earlier question on this topic (see S.Lott's comment on your question).

John Machin