import sys import re def search_key(file,key): s=' '+key+' ' p=re.compile(s) for line in file: val=p.findall(line) if(val): print(line) def main(): argc_len=len(sys.argv) if(argc_len!=3): print("参数错误,请输入正确的参数") sys.exit() path=sys.argv[1] key_word=sys.argv[2] logfile = open(path, "r", -1, "utf-8") search_key(logfile, key_word) main()
该文件命名为test.py
Windows cmd 控制台输入命令 python test.py search.txt py
search.txt 是待查找的文件 py 是要查找的关键字