Let's look at a code:
Yesterday2 is the file I created last post, in order to make it easier for everyone to see I copied the original code to the following:
Coding=utf-8
F=open ("Yesterday2", "R", encoding= "Utf-8")
F_new=open ("Yesterday2.bak", "W", encoding= "Utf-8")
For line in F:
If "Yesterday when I was young and frivolous" in line:
Line=line.replace ("Yesterday when I was young and frivolous", "yesterday when Vedasian young frivolous")
F_new.write (line) Else:f_new.write (line)
F.close ()
F_new.close () Of course "R" is the meaning of the read file, "W" is the meaning of the write #coding=utf-8
F=open ("Yesterday2", "R", encoding= "Utf-8")
F_new=open ("Yesterday2.bak", "W", encoding= "Utf-8")
For line in F:
If "Yesterday when I was young and frivolous" in line:
Line=line.replace ("Yesterday when I was young and frivolous", "yesterday when Vedasian young frivolous")
F_new.write (line)
F.close ()
F_new.close ()
In fact, in order to alleviate the cumbersome code, so the code is merged after the removal of the other, of course, sometimes forget the source file and write directly to replace the content
Vedasian--python to file operations 2--write and modify