标签:修改 coding 文件 enc odi 歌词 line write import
#文件操作,歌词hello
import os
lyric = open("E:/python_pj/test/hello.txt",mode="r")
data = lyric.read()
data = data.replace("hello","HELLO")
lyric.close()
lyric = open("E:/python_pj/test/hello.txt",mode="w")
lyric.write(data)
lyric.close()
#逐行修改
import os
lyric = open("E:/python_pj/test/hello.txt",mode="r",encoding="utf-8")
lyric_new = open("E:/python_pj/test/hello.txt",mode="w",encoding="utf-8")
for line in lyric:
if "Hello" in line:
line = line.replace("Hello","HELLO")
lyric_new.write(line)
lyric.close()
lyric_new.close()
os.remove("E:/python_pj/test/hello.txt")
os.remove("E:/python_pj/test/hello_new.txt","E:/python_pj/test/hello.txt")
标签:修改 coding 文件 enc odi 歌词 line write import
原文地址:https://www.cnblogs.com/elontian/p/9461625.html