f = open("yesterday","r",encoding="utf-8") f_new=open("yesterday2.bak","w",encoding="utf-8") for line in f: if "等待我最喜爱的歌" in line: line = line.replace("等待我最喜爱的歌","等待Alex最喜爱的歌") f_new.write(line) f.close() #要注意跳出for循环再close,否则会报错 f_new.close()
时间: 2024-10-10 07:40:19