Assuming that the filename is foo.py
, the below snippet
import sys
print sys.argv[0][:-3]
or
import sys
print sys.argv[0][::-1][3:][::-1]
As for other extentions with more characters, for example the filename foo.pypy
import sys
print sys.argv[0].split('.')[0]
If you want to extract from an absolute path
import sys
print sys.argv[0].split('/')[-1].split('.')[0]
will output foo