def replaceInFile(filename, oldToNew):
 os.rename(filename, (filename + '.bak'))
 f = open((filename + '.bak'))
 d = f.read()
 f.close()
 for (k, v) in oldToNew.items():
  d = d.replace(k, v)
 f = open((filename + '.new'), 'w')
 f.write(d)
 f.close()
 os.rename((filename + '.new'), filename)
 os.unlink((filename + '.bak'))

def replaceInFile ( filename , oldToNew ) : os . rename ( filename , ( filename + '.bak' ) ) f = open ( ( filename + '.bak' ) ) d = f . read ( ) f . close ( ) for ( k , v ) in oldToNew . items ( ) : d = d . replace ( k , v ) f = open ( ( filename + '.new' ) , 'w' ) f . write ( d ) f . close ( ) os . rename ( ( filename + '.new' ) , filename ) os . unlink ( ( filename + '.bak' ) )
i replace the text oldstr with newstr in filename using science .