3
Good evening, I am having problems while removing a line from a txt file, the deleting function of the code is responsible for removing a user, however while trying to remove the entire file is deleted.
def deleta():
    usuario = input("\nUsuário: ") + "\n"
    senha = input("Senha: ")
    confirma = input("Confirma a exclusão de "+usuario+"? \ns/n: ")
    confirma.lower()
    if confirma == 's' or 'sim':
        with open("users.txt", 'r') as users:
            loginAndPass = users.readlines()
            # Proucura pelo login
            if usuario in loginAndPass:
                posi = loginAndPass.index(usuario)
                # autentica
                if posi % 2 != 0:
                    if testSHA512(senha, loginAndPass[int(posi) + 1].replace('\n', '')):
                        users = open("users.txt", 'w')
                        while posi in loginAndPass:
                            loginAndPass.remove(posi)
                            users.writelines(loginAndPass)
                            users.close()
                        print("\nUsuario removido\n")
                    else:
                        print("\nUsuário ou Senha inválidos\n")
                else:
                    print("\nUsuário ou Senha inválidos\n")
            else:
                print("\nUsuário ou Senha inválidos\n")
    elif confirma == 'n' or 'nao':
        print("passou")
    else:
        print("Opção inválida\nPrograma finalizado!")
						
The line
confirma.lower()is useless becausestr.lower()returns a copy of the converted string to lowercase and you’re not saving the result. What you want to do seems to beconfirma = confirma.lower(). And the lineif confirma == 's' or 'sim':doesn’t do what you think it does, the right thing would beif confirma == 's' or confirma == 'sim':orif confirma in ('s', 'sim'):.– fernandosavio
Thanks for the tip, I made the changes nescessarias.
– Thierry Braga
One more tip, you opened the file for reading on this line:
open("users.txt", 'r') as usersbut is trying to record inusers.writelines(loginAndPass)– fernandosavio