def file_replace(filename,old_word,new_word): f_read=open(filename) count=0 context=[] for eachLine in f_read: #print(eachLine) count+=eachLine.count(old_word) eachLine=eachLine.replace(old_word,new_word) context.append(eachLine) f_read.close() decide=input("共计%s处%s要替换成%s,替换吗?(yes or no)"%(count,old_word,new_word)) print(decide.lower()) if decide.lower()=="yes": f_write=open(filename,"w") f_write.writelines(context) f_write.close() filename=input("请输入文件名:") old_word=input("请输入旧文字:") new_word=input("请输入新文字:") file_replace(filename , old_word , new_word)
时间: 2024-10-11 00:22:14