python--文件操作删除某行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python--文件操作删除某行相关的知识,希望对你有一定的参考价值。
方法一:
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--文件操作删除某行的主要内容,如果未能解决你的问题,请参考以下文章