If you are trying to compare the user's input with the contents of a single-line text file that the user and password store, they are making an error when reading it:
To get the content of the file you must do something like user = user__file2.read()
, where you assign the string to the variable user
. If you only have one line you can use readline()
.
Remember to delete the line break when comparing if it exists (depends how you think the file), you can use str.strip()
for it:
You can use with
to ensure the closing of the files without having to make it explicit.
If you use Python 3 as you indicate, input
already returns a string. The casting is not necessary therefore.
def login():
input_user = input("Nombre de usuario: ")
input_pwd = input("Contraseña : ")
with open("login/users.txt") as f:
user = f.readline().strip("\n\r")
with open("login/passwords.txt") as f:
pwd = f.readline().strip(a.strip("\n\r")
if user == input_user and pwd == input_pwd:
print("Credenciales de acceso válidas")
print("Acceso completo obtenido")
else:
print("Credenciales de acceso no válidas", datetime.date.today())
exit()
Remember that storing credentials in a plain text file is very insecure.
Edit:
As very well appreciated @ PatricioMoracho, there is an error in the logic of your conditional that causes that if a valid user is entered but the password is incorrect, the login is validated. I have chosen not to report separately the validity of the user, although if you want this behavior as you seem to indicate in your code you can do something like:
if user == input_user:
print("Nombre de usuario correcto")
if pwd == input_pwd:
print("Credenciales de acceso válidas\nAcceso completo obtenido")
return
else:
print("Contraseña incorrecta")
else:
print("Usuario incorrecto")
print("Credenciales de acceso no válidas", datetime.date.today())
exit()