今天学到了python 打开并修改文件,需要的步骤是:
import os import time with open("薛之谦歌单.txt",mode="r",encoding="utf-8") as f1,\ open("薛之谦_副本.txt",mode="w",encoding="utf-8") as f2: for line in f1: line = line.strip() if line.startswith("爱"): line = line.replace("爱","恨") f2.write(line) f2.write("\n") time.sleep(3) os.remove("薛之谦歌单.txt") time.sleep(3) os.rename("薛之谦_副本.txt","薛之谦歌单.txt")