在文件中搜索术语

问题描述 投票:0回答:1

我的脚本有问题。我想在搜索列表中定义的不同文件中找到一些术语。但是 - 它不会找到这些术语,即使它们肯定包含在这些文件中。我检查了一下。

我错过了什么吗?

path = '/mypath'

for filename in glob.glob(os.path.join(path, '*.txt')):
    with open(filename) as infp:
        mylist = infp.read().splitlines() 
        for word in mylist:
            searchlist = ["term1", "term2", "term3"]
            for i in searchlist:
                if i in word:
                    print ('found')
                else: 
                    print ('not found')
python file search
1个回答
1
投票

这可能有所帮助

path = '/mypath'

for filename in glob.glob(os.path.join(path, '*.txt')):
    with open(filename) as infp:
        data = infp.read()    #Just Read the content
        searchlist = ["term1", "term2", "term3"]
        for i in searchlist:
            if i in data:  #Check if search term in content. 
                print('found')
            else: 
                print('not found')
© www.soinside.com 2019 - 2024. All rights reserved.