Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文件中搜索术语_Python_File_Search - Fatal编程技术网

Python 在文件中搜索术语

Python 在文件中搜索术语,python,file,search,Python,File,Search,我的脚本有问题。我想在搜索列表中定义的不同文件中查找一些术语。但是,它不会找到这些术语,即使它们确实包含在这些文件中。我查过了 我错过什么了吗 路径='/mypath' 对于glob.globos.path.joinpath,'*.txt'中的文件名: 使用openfilename作为infp: mylist=infp.read.splitlines 对于mylist中的单词: 搜索列表=[term1、term2、term3] 对于搜索列表中的i: 如果我说: 打印“找到” 其他: 打印“未找到

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

我错过什么了吗

路径='/mypath' 对于glob.globos.path.joinpath,'*.txt'中的文件名: 使用openfilename作为infp: mylist=infp.read.splitlines 对于mylist中的单词: 搜索列表=[term1、term2、term3] 对于搜索列表中的i: 如果我说: 打印“找到” 其他: 打印“未找到” 这可能会有帮助

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')
我的列表是你的台词列表,而不是单词列表。您需要拆分行以获取单词,然后应用搜索。看,看看这个