Python script breaks after first iteration
Question: I am trying to do the following in python. Read xml file with usernames, pw,
email addresses and so on. I then want to iterate through the passwords and
try to find them in a different file. if there is a match, print the username
and password. this is what i have so far:

    
    
    import xml.etree.ElementTree as ET
    
    tag = "multiRef"
    
    
    tree = ET.parse('user.xml')
    pwlist = open('swapped.txt', 'r')
    
    for dataset in tree.iter(tag):
        password = dataset.find("password")
        pw = password.text
        user = dataset.find("username").text
        if pw in pwlist.read():
            print user
            print pw
    

Unfortunately, the script only prints one result and ends with no error or
anything. I know, that there have to be at least 250 results... Why is it
stopping after one result? Absolute python newb, a detailed explanation would
be much appreciated!


Answer: `if pw in pwlist.read()` shouldn't be inside the loop. The first time through
the loop, read() will return the entire file. The second time through it will
return nothing because you are at END OF FILE.

Read the contents into a list prior to the loop and then refer to the list
inside the loop.

Also consider the pattern `with open(...) as f` to make sure you are closing
the file, since I don't see an explicit close().

    
    
    with open('swapped.txt', 'r') as f:
       pw_list = f.readlines()
    
    for ...
    

