def filename_search_replace(sr_pairs, filename, backup=False):
 in_txt = open(filename, u'rt').read((-1))
 out_txt = in_txt[:]
 for (in_exp, out_exp) in sr_pairs:
  in_exp = re.compile(in_exp)
  out_txt = in_exp.sub(out_exp, out_txt)
 if (in_txt == out_txt):
  return False
 open(filename, u'wt').write(out_txt)
 if backup:
  open((filename + u'.bak'), u'wt').write(in_txt)
 return True

def filename_search_replace ( sr_pairs , filename , backup = False ) : in_txt = open ( filename , u'rt' ) . read ( ( - 1 ) ) out_txt = in_txt [ : ] for ( in_exp , out_exp ) in sr_pairs : in_exp = re . compile ( in_exp ) out_txt = in_exp . sub ( out_exp , out_txt ) if ( in_txt == out_txt ) : return False open ( filename , u'wt' ) . write ( out_txt ) if backup : open ( ( filename + u'.bak' ) , u'wt' ) . write ( in_txt ) return True
search and replace for expressions in files .