Write a function called replace() that takes pattern string, replacement string and 2 files as argument. The function should read the first file and write the content into second file (creating it, if necessary). If the pattern string appears anywhere in first file, it should be replaced by replacement string in second file.


In [ ]:
def replace(pattern,replacement,file1,file2):
    '''Function to replace pattern string in file1 and save replaced strings in file2'''
    f1 = open(file1,'r')
    f2 = open(file2,'w')
    content = f1.read()
    content = content.replace(pattern,replacement)
    f2.write(content)

In [ ]: