我正在编写一个脚本,该脚本在目录中的文本文件中搜索某些单词。如果返回“True”,那么它将打印找到它的文本文件。有没有办法返回/打印它实际找到的关键字?谢谢!
from glob import glob
def main():
directory = "C:\\Files Folder\\*.txt"
filelist = glob(directory)
keywords = ("Hello", "Goodbye", "fake")
for textfile in filelist:
f = open(textfile, 'r')
for line in f:
if any(s in line for s in keywords):
print "Keyword Found!", f.name
if __name__ == '__main__':
main()