with open("文件路径","模式","编码") as file ("r" 是读,"w"是写)
with open(r"D:\python-installer\python-code\pythonProject\src\关于文件操作练习/人物介绍.txt","r",encoding="utf-8") as file1: data=file1.read() print(type(data)) #检验读取到的文章数据是什么类型(字符串类型) newData=data.replace("刘小言","刘晓言") with open(r"D:\python-installer\python-code\pythonProject\src\关于文件操作练习/人物介绍.txt","w",) as file2: file2.write(newData)