Get name of current script in Python


I'm trying to get the name of the Python script that is currently running.

I have a script called and I'd like to do something like this in order to get the script name:

print Scriptname
8/27/2019 11:59:06 PM

Accepted Answer

Use __file__. If you want to omit the directory part (which might be present), you can use os.path.basename(__file__).

7/25/2017 12:29:37 PM

import sys
print sys.argv[0]

This will print for python, dir/ for python dir/, etc. It's the first argument to python. (Note that after py2exe it would be foo.exe.)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow