修改文件内容--初学很low的写法

 1 import os
 2 f = open('yy.txt','r',encoding='utf-8')
 3 f_new = open('yy.bak','w+',encoding='utf-8')
 4 for line in f:
 5     if '小光头' in line:
 6         line = line.replace('小光头','大头仔')
 7     f_new.write(line)
 8 f.close()
 9 f = open('yy.txt','w',encoding='utf-8')
10 f_new.seek(0)
11 for line_new in f_new:
12     f.write(line_new)
13 f.close()
14 f_new.close()

 

posted @ 2017-05-03 14:13  友谅  阅读(124)  评论(0编辑  收藏  举报