码迷,mamicode.com
首页 > 其他好文 > 详细

文件操作

时间:2018-08-12 00:34:32      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:修改   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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!