Reputation: 17
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
Upvotes: 1
Views: 1874
Reputation: 18106
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
Upvotes: 3