Search for terms in file

235
March 01, 2018, at 4:34 PM

I am having problems with my script. I want to find some terms in different files, defined in a searchlist. However - it won't find those terms, even if they are definitely included in those files. I checked it.

Am I missing something?

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')
Answer 1

This might help

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')
Rent Charter Buses Company
READ ALSO
How to make Tic Tac Toe game using Reinforcement learning in Python [on hold]

How to make Tic Tac Toe game using Reinforcement learning in Python [on hold]

I googled for 40 hours, but can't move even a single step forward, this is what I have done, so far:

170
Reconnecting a Bluetooth device using socket library (RFCOMM mode) in python 3?

Reconnecting a Bluetooth device using socket library (RFCOMM mode) in python 3?

I am trying to connect to a Bluetooth GPS unit from a Raspberry Pi3 using the socket library in python 3I am able to connect and get data flowing the first time but if I disconnect and then try reconnecting I get:

302
I heard “support function” method using in stack overflow [on hold]

I heard “support function” method using in stack overflow [on hold]

I heard "support function" method using the stack overflowMy question is "What is support function and how could we use support function?" Give an example to use "support function"

169
Producing combinations of lambda functions compositions

Producing combinations of lambda functions compositions

I am facing a challenging issue in order to make my Python3 code more elegant

180