#!/usr/bin/env python

import os,re,sys

def fandr(filename,what,withwhat):
   f = open(filename,"rb")
   s = os.fstat(f.fileno())[6]
   inp = f.read(s) # Read full file
   f.close()
   r = re.compile(what,re.IGNORECASE)
   out = r.sub(withwhat,inp)
   f = open(filename,"wb")
   f.write(out)
   f.close()

if __name__ == "__main__":
   if len(sys.argv) != 4:
      print "Usage: %s <filename> <from> <to>"
      print
      print "replace occurences of <from> with <to> in <filename>"
      print "   <from> and <to> should have same length"
      exit(1)
   if len(sys.argv[2]) != len(sys.argv[3]):
      print "<from> and <to> should have same length"
   fandr(sys.argv[1],sys.argv[2],sys.argv[3])

