Python 3.4 does not read more than one line from a text file
<p>I am writing a python(3.4) code which uses a basic authentication. I have stored the credentials i.e the username &amp; password in a text file(abc.txt).<br>
Whenever I login, the code accepts only the first line of the text file &amp; ignores the rest of the credentials and gives incorrect credentials error.</p>

<p>My code:</p>

<pre><code>with open('abc.txt') as f:
    credentials = [x.strip().split(':') for x in f.readlines()]

for username, password in credentials:

    user_input = input('Please Enter username: ')

    if user_input != username:

        sys.exit('Incorrect incorrect username, terminating... \n')


    user_input = input('Please Enter Password: ')



    if user_input != password:

        sys.exit('Incorrect Password, terminating... \n')



    print ('User is logged in!\n')  
</code></pre>

<p>abc.txt:</p>

<pre><code>Sil:xyz123
smith:abc321
</code></pre>

<p>That is happening because you are only checking the first line. Currently the user can only enter credentials that match the first line in the text file, otherwise the program will exit. You should create a dictionary with the username and password and then check if a username is in that dictionary instead of iterating over the list of credentials.</p>

<pre><code>with open('abc.txt') as f:
    credentials = dict([x.strip().split(':') for x in f.readlines()]) # Created a dictionary with username:password items

username_input = input('Please Enter username: ')

if username_input not in credentials:  # Check if username is in the credentials dictionary

        sys.exit('Incorrect incorrect username, terminating... \n')

password_input = input('Please Enter Password: ')

if password_input != credentials[username_input]: # Check if the password entered matches the password in the dictionary

        sys.exit('Incorrect Password, terminating... \n')

print ('User is logged in!\n')  
</code></pre>

python python-3.4