SubniC SubniC - 5 months ago 14
Python Question

Get the name of current script with Python

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

For example, I have a script called

foo.py
and I would like to do something like this inside it:

print Scriptname


and get:
foo.py
.

Answer

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