python删除行_python--文件操作删除某行

随笔 -

54

文章 -

4

评论 -

3

方法一:

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

withopen("fileread.txt","r",encoding="utf-8") as f:

lines= f.readlines()

#print(lines)

withopen("fileread.txt","w",encoding="utf-8") as f_w:

for linein lines:

if "tasting123" in line:

continue

f_w.write(line)

posted @

2017-06-16 10:53

Mr.joey

阅读(22742)

评论(0)

编辑

收藏

Copyright © 2020 Mr.joey

Powered by .NET 5.0.0 on Kubernetes

你可能感兴趣的:(python删除行)