文件的修改
#文件的修改
#方法一 适用于小文件修改
# import os
# with open("5","r",encoding="utf-8") as f_r,open("6","w",encoding="utf-8") as f_w:
# data=f_r.read()
# f_w.write(data.replace("alex","Alex_sb"))
# os.remove("5")
# os.rename("6","5")
#方法二 适用于大文件
import os
with open("5","r",encoding="utf-8") as f_r,open("6","w",encoding="utf-8") as f_w:
for line in f_r:
f_w.write(line.replace("Alex_sb","sb_Alex_sb"))
os.remove("5")
os.rename("6","5")
#方法一 适用于小文件修改
# import os
# with open("5","r",encoding="utf-8") as f_r,open("6","w",encoding="utf-8") as f_w:
# data=f_r.read()
# f_w.write(data.replace("alex","Alex_sb"))
# os.remove("5")
# os.rename("6","5")
#方法二 适用于大文件
import os
with open("5","r",encoding="utf-8") as f_r,open("6","w",encoding="utf-8") as f_w:
for line in f_r:
f_w.write(line.replace("Alex_sb","sb_Alex_sb"))
os.remove("5")
os.rename("6","5")