python删除文件中含"指定内容关键字"的一行

 python运行版本:2.7  (3以上的版本需将里面不兼容的语句改写一下)

#!/bin/env python
 
import shutil, sys, os
 
darray = [
	"xxxxxxxxx",
	";;;;;;;;;"
]
 
def isInArray (array, line):
	for item in array:
		if item in line:
			return True
	return False
 
 
if __name__ == '__main__':
	argv = sys.argv
	argc = len(argv)
	if argc < 2:
		print "Usage: %s <file>" %(os.path.basename(argv[0]))
		exit()
	fname = argv[1]
	fresult = fname + ".result"
	
	with open(fname, 'r') as f:
		with open(fresult, 'w') as g:
			for line in f.readlines():
				if not isInArray(darray, line):
					g.write(line)

 

posted @ 2020-04-23 11:26  龙雪  阅读(1646)  评论(0编辑  收藏  举报