1

I'm very new to Python and I'm trying to solve the following problem. I want to iterate a List with words and lookup it in a file and if found return me the line number where the words are found.

files = glob.iglob(os.path.join(in_dir, '*.*'))
List = ['word1', 'word2',...'wordn']

for index, word in enumerate(List):
    print(index, word)

    for item in files: # Iterate all files in Input directory
        filename = os.path.basename(item) # Copy the filename without the path
        print('file: {0}'.format(filename))
        with open(item, 'r') as f:
            for num, line in enumerate(f, 0):
                if word in line:
                    print('num: {0}, line: {1}'.format(num, line))

But it doesn't seem to work, it only searched the first value o the list. What I want to do is to search all the List values in every file.

Lauraducky
  • 674
  • 11
  • 25
Maki
  • 201
  • 5
  • 11

1 Answers1

1

I think your holdup is that iglob returns a generator. It is a one-use iterator that gets used during the first "for item in files" loop. You need to either replace iglob with glob (returns a list instead of a one-time use iterator), or re-invoke iglob for every loop.

Read more about generators:

And glob documentations:

Hope this helps set you in the right direction!

Edit (Note): The advantage of iglob over glob is that it doesn't work with the entire list in memory. If you have a really huge list (lots of items * size in memory per item), then iglob may be expected to have performance advantages over glob.

mathewguest
  • 630
  • 5
  • 10