码迷,mamicode.com
首页 > 编程语言 > 详细

Python__替换文件操作

时间:2017-07-13 21:42:52      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:else   open   port   span   nbsp   lin   pen   class   name   

##此时需要导入os模块
import os
with open(old.txt,r,encoding = utf-8) as read_f,open(new.txt,w,encoding = utf-8) as write_f:
    for line in read_f:
        if mengzhu in line:
            line.replace(mengzhu,dreamkily)
            write_f.write(line)
        else:
            write_f.write(line)
os.remove(old.txt)
os.rename(new.txt,old.txt)

 

Python__替换文件操作

标签:else   open   port   span   nbsp   lin   pen   class   name   

原文地址:http://www.cnblogs.com/wangmengzhu/p/7163087.html

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