首页 > 开发 > Python > 正文

python如何删除文件里包含关键词的行

2017-09-06 19:38:46  来源:网友分享

要把一个文件里的所有含有/local/server的行删除.

解决方案

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