def fix_script(path):
 if os.path.isfile(path):
  script = open(path, 'rb')
  try:
   firstline = script.readline()
   if (not firstline.startswith(binary('#!python'))):
    return False
   exename = sys.executable.encode(sys.getfilesystemencoding())
   firstline = ((binary('#!') + exename) + binary(os.linesep))
   rest = script.read()
  finally:
   script.close()
  script = open(path, 'wb')
  try:
   script.write(firstline)
   script.write(rest)
  finally:
   script.close()
  return True

def fix_script ( path ) : if os . path . isfile ( path ) : script = open ( path , 'rb' ) try : firstline = script . readline ( ) if ( not firstline . startswith ( binary ( '#!python' ) ) ) : return False exename = sys . executable . encode ( sys . getfilesystemencoding ( ) ) firstline = ( ( binary ( '#!' ) + exename ) + binary ( os . linesep ) ) rest = script . read ( ) finally : script . close ( ) script = open ( path , 'wb' ) try : script . write ( firstline ) script . write ( rest ) finally : script . close ( ) return True
replace #!python with #!/path/to/python return true if file was changed .