首页 > 编程语言 > 详细

python--文件操作删除某行

时间:2017-06-16 11:20:13      阅读:500      评论:0      收藏:0      [点我收藏+]

方法一:

 

import shutil
with open(‘/path/to/file‘, ‘r‘) as f:
    with open(‘/path/to/file.new‘, ‘w‘) as g:
        for line in f.readlines():
            if ‘/local/server‘ not in line:             
                g.write(line)
shutil.move(‘/path/to/file.new‘, ‘/path/to/file‘)

 

方法二:

 

将文本中的 tasting123删除 

1
2
3
4
5
6
7
8
with open("fileread.txt","r",encoding="utf-8") as f:
    lines = f.readlines()
    #print(lines)
with open("fileread.txt","w",encoding="utf-8") as f_w:
    for line in lines:
        if "tasting123" in line:
            continue
        f_w.write(line)

 

python--文件操作删除某行

原文:http://www.cnblogs.com/nopnog/p/7026390.html

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