0

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')
daniel_e
  • 257
  • 1
  • 11
  • `mylist` is a list of your lines, not words. You need to `split` the line to get words and then apply the search. See https://docs.python.org/3/library/stdtypes.html#str.splitlines – PhillipD Mar 01 '18 at 10:45
  • Check out this SO https://stackoverflow.com/a/4944929/1225070 – Aefits Mar 01 '18 at 10:47

1 Answers1

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')
Rakesh
  • 81,458
  • 17
  • 76
  • 113