Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

read username and password from text file

I am trying to make a username and password that gets taken from a text file. The username and password is added at the beginning and used after. I add a username and password at the start and it works. It adds it to the text document but it says that it isn’t on the document when i enter the 2 previously created credentials. I put the part i belive is giving issues in **. Any way to make this work properly? If my point isn’t clear i can specify more if necessary. Thanks.

import time
import sys

text1 = input("\n Write username ")
text2 = input("\n Write password ")
saveFile = open('usernames+passwords', 'r+')
saveFile.write('\n' + text1 + '\n' + text2 + '\n')
uap = saveFile.read()
saveFile.close()
max_attempts = 3
attempts = 0

while True:
    print("Username")
    username = input("")

    print("Password")
    password = input("")

    *if username in uap and password in uap:
        print("Access Granted")*
    else:
        attempts+=1
        if attempts >= max_attempts:
            print(f"reached max attempts of {attempts} ")
            sys.exit()
        print("Try Again (10 sec)")
        time.sleep(10)
        continue
    break

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

saveFile.write writes to the end of the file, so the file cursor points to the end of the file.
saveFile.read() reads from the current position to the end (docs).

You need to move the file cursor to the beginning of the file, before reading:

text1 = 'foo'
text2 = 'bar'

saveFile = open('/tmp/usernames+passwords', 'r+')
saveFile.write('\n' + text1 + '\n' + text2 + '\n')
saveFile.seek(0)
uap = saveFile.read()
print(uap)

Out:

foo
bar
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading