def _detect_filename(ext):
 import inspect
 from os.path import isfile, dirname, basename, splitext, join
 frame = inspect.currentframe()
 while (frame.f_back and (frame.f_globals.get('name') != '__main__')):
  frame = frame.f_back
 filename = frame.f_globals.get('__file__')
 if (filename and isfile(filename)):
  (name, _) = splitext(basename(filename))
  return join(dirname(filename), ((name + '.') + ext))

def _detect_filename ( ext ) : import inspect from os . path import isfile , dirname , basename , splitext , join frame = inspect . currentframe ( ) while ( frame . f_back and ( frame . f_globals . get ( 'name' ) != '__main__' ) ) : frame = frame . f_back filename = frame . f_globals . get ( '__file__' ) if ( filename and isfile ( filename ) ) : ( name , _ ) = splitext ( basename ( filename ) ) return join ( dirname ( filename ) , ( ( name + '.' ) + ext ) )
detect filename from the name of the script being run .