You actually have several options. Of these I present to you three:
import os
print(os.path.basename(__file__))
import sys
print (sys.argv[0])]
print (__file__)
There is a discussion interesting in the OS regarding the pros and cons of the use of each one. Worth consulting.
Where:
__file__
is the name of the file path from which the module was loaded, if it was loaded from a file. The __file__
attribute can
be missing for certain types of modules such as C modules that are
statically linked to the interpreter; for extension modules
dynamically loaded from a shared library, is the
path name of shared library file.
argv[0]
is the name of the script (depends on the operating system if this is a
path name or not). If the command was executed using the
option -c
command line for the interpreter, argv[0]
will be set to the string '-c
'. If no script name has been passed to the
python interpreter, argv[0]
is the empty string.
The information that made up this reply was removed from here. I thank the user Yoel.