How to Get Name of Current Script in Python

Created
Modified

Using Module Attributes

You can use __file__ to get the name of the current file. When used in the main module, this is the name of the script that was originally invoked.

See the following example:

#!/usr/bin/python3

# Import module
import os

print(__file__)

# omit the directory part
print(os.path.basename(__file__))
/home/python/demo.py
demo.py

Related Tags

#name# #script#