首页 > 其他 > 详细

文件操作

时间:2018-08-12 00:07:57      阅读:237      评论:0      收藏:0      [点我收藏+]
#文件操作,歌词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")

文件操作

原文:https://www.cnblogs.com/elontian/p/9461625.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!