TypeError: integer argument expected, got 'str'

3

The error occurs in the login of a program, I have been reading and I found a solution with the open, but it does not work for someone to help me, thanks in advance.

def login():

    user2=str(input("Nombre de usuario:"))
    user__file2=open("login/users.txt" ,mode= "r")
    user__file2.read(user2)
    user__file2.close()

    contraseña2=str(input("Contraseña : "))
    pwd__file2=open("login/passwords.txt" ,mode= "r")
    pwd__file2.read(contraseña2)
    pwd__file2.close()

    if user2 == user__file2:

        print("Nombre de usuario correcto")
        if pwd__file2 == contraseña2:
            print("Acceso completo obtenido")
    else:
        print("Credenciales de acceso no validas", datetime.date.today())

        exit()
    
asked by x5678 04.01.2018 в 15:16
source

1 answer

1

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()
    
answered by 04.01.2018 / 15:51
source