def set_config_value(filepath, key, value):
 replacement_line = ('%s = %s\n' % (key, value))
 match = re.compile(('^%s\\s+=' % key)).match
 with open(filepath, 'r+') as f:
  lines = f.readlines()
  f.seek(0, 0)
  f.truncate()
  for line in lines:
   f.write((line if (not match(line)) else replacement_line))

def set_config_value ( filepath , key , value ) : replacement_line = ( '%s = %s\n' % ( key , value ) ) match = re . compile ( ( '^%s\\s+=' % key ) ) . match with open ( filepath , 'r+' ) as f : lines = f . readlines ( ) f . seek ( 0 , 0 ) f . truncate ( ) for line in lines : f . write ( ( line if ( not match ( line ) ) else replacement_line ) )
set key = value in config file .