python get script name

Solutions on MaxInterview for python get script name by the best coders in the world

showing results for - "python get script name"
Nathanael
16 Jan 2020
1# Option 1: Works for Python 3.4 +
2from pathlib import Path
3Path(__file__).name		# ScriptName.py
4Path(__file__).stem		# ScriptName
5
6# Option 2: use `os` library 
7import os
8os.path.basename(__file__)							# ScriptName.py
9os.path.splitext(os.path.basename(__file__))[0]		# ScriptName
Tim
15 Jul 2019
1import os
2
3os.path.basename(__file__)
Juan
16 Mar 2017
1Use __file__. If you want to omit the directory part (which might be present), you can use os.path.basename(__file__)