I have a text file (file1.txt) containing pattern like this.
****
****
****
****
I want to replace it by **
. I can use command something like this sed -i 's/\*{4}/**/' file1.txt
in linux to get in place replacement. But I want to do this operation in Windows environment using python script.
import re
with open ('file1.txt') as fil1:
for line in fil1:
re.sub('^\*{3}[*]*','**',line)
But this script does not seem to replace ****
by **
in place in the file. How can I get in place file replacement (similar to sed
command) in python?
Edit:
I do not want to read file line by line, replace text and write the line into another file. I want to do something like sed -i
to do in place file replacement in python using regular expressions.