str1 = input('需要修改的部分:')
str2 = input('修改成;')
print(type(str1))
with open('lock.txt','r+',encoding='utf-8') as f1,\
open('123.txt','r+',encoding='utf-8') as f2:
for i in f1:
if str1 in i:
i = i.replace(str1, str2)
f2.write(i)
需要可以在用w+把新的文件写回去
转载于:https://www.cnblogs.com/czh96/p/11251075.html