Reputation: 2689
I can't see the problem here and it is driving me insane. I'm looping through 2 text files. Some lines in each file match and some don't. What I am doing is looping over file1. For each line in that file, loop over file2 and compare each element to see if they are the same. What's happening is my loop is stopping after the first loop through file1. Here is my code:
while f < 50:
for line in file1:
for name in file2:
if name == line:
print 'a match was found'
f+=1
The while loop comes from somewhere else but it is working fine. I just included it for context. The problem is file1 only gives me the first line, compares it to all of the 'names' in file2 then stops instead of repeating the process for the next line in file1. Am I missing something glaringly obvious?
EDIT: If I put a print statement in after the first for loop and comment out the other for loop it loops through the whole first file
Upvotes: 6
Views: 14722
Reputation: 1121914
You cannot loop through a file and then loop through the same file again without seeking to the start.
Either re-open file2, call .seek(0)
on file2 or load all lines into a list and loop over that instead.
In your specific case, using a set
for the names is probably going to be the fastest:
names = set(name.strip() for name in file2)
while f < 50:
for line in file1:
if line.strip() in names:
f += 1
You can do the same with the lines in file1 and do a set intersection, provided that lines are unique in both file1 and file2.
Upvotes: 12
Reputation: 309929
The problem could be that once you've iterated over file2
, it is exhausted so your inner for loop is not executing any longer (since there's nothing left in file2
to iterate over). You can close/reopen file2 each time through the loop, or you can seek back to the beginning before that loop is executed.
A slightly better approach would be to use sets (if the files aren't too big and you're not concerned about duplicates within a file or order):
matches = set(file1).intersection(file2)
This should read only file1 into memory and do the loop over file2 implicitly.
Upvotes: 4
Reputation: 622
Depending on the size of the files, you can use the readlines()
function to read the lines of each file into a list.
Then, iterate over these lists. This will ensure that you do not have problems with the current position of the file position.
Upvotes: 0
Reputation: 10493
After first time the inner loop is finished, the inner iterator over file2 reached the end so the solution is to point inner iterator of file2 to file's beginning each time, for example:
while f < 50:
for line in file1:
file2.seek(0, 0)
for name in file2:
if name == line:
print 'match!'
Upvotes: 3