方法一:

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')

方法二:

with open("fileread.txt","r",encoding="utf-8") as f:
  lines = f.readlines()
with opren(
"fileread.txt","w",encoding="utf-8") as f_w:
 for line in lines:
    if "行包含的信息“ in line:
      continue
    f_w.write(line)
    
 
 
参考原文:https://www.cnblogs.com/nopnog/p/7026390.html