Yes, file objects are iterators.
Like all iterators, you can iterate over them only once, after which the iterator is exhausted. The file read pointer is at the end of the file. Reopen the file or use .seek(0) to rewind the file pointer if you need to loop again.
As an alternative, try to avoid looping around the file twice; extract what you need into another data structure (list, dictionary, set, heap, etc.) during the first cycle.
source share