简体   繁体   English

我正在 python 中开发身份验证程序,但我遇到了数据库问题

[英]I am developing an authentication program in python but I am having database problems

I am developing a login system but im having trouble with the user and password storage part.我正在开发一个登录系统,但我在用户和密码存储部分遇到了问题。 I made a list and i outsourced it to a text file and it writes everything in it but every time i run the program again it erases everything it stored previously.我制作了一个列表,并将其外包给一个文本文件,它会将所有内容写入其中,但每次我再次运行该程序时,它都会删除之前存储的所有内容。 Here is the code i wrote until now.这是我到现在为止写的代码。 Anything else i am doing wrong, please let me know, i am happy to get a more skilled person's opinion on this.还有什么我做错了,请告诉我,我很高兴得到更熟练的人对此的看法。

done = True

while done == True:
    user = input('Create Username: ')

    from pwgen import genPass

    break

while done == True:    
    password = input('Create Password: ')
    done = False

    store_user = []
    store_pass = []

    store_userpass = []

    if user in store_user:
        print("That user already exists")
    else:
        store_user.append(user)
        store_pass.append(password)

        outF = open('user.txt', 'w')
        outf= open('pw.txt', 'w')

        for line in store_user:
            outF.write(line)
            outF.write('\n')

        for line1 in store_pass:
            outf.write(line1)
            outf.write('\n')

    outF.close()
    outf.close()

    done = False

while True:
    userguess=""
    passwordguess=""
    key=""
    while (userguess != user) and (passwordguess != password):
        userguess = input('User Name: ')
        passwordguess = input('Password: ')

        if userguess != user:
            print("Error: Unknown username.")
        if userguess == user and passwordguess != password:
            print("Error: Incorrect password. Try again.")
        if userguess != user and passwordguess != password:
            print("Error: Unknown user and password combination.")
        if userguess == user and passwordguess == password:
            print("Welcome,", user, ". Type lock to lock this user.")
    break

while key != "lock":
    key = input()

You open the files user.txt and pw.txt in write mode (the second parameter in the open function), which recreates the file each time, instead of append mode, which will allow you to append data to your files.您以write模式( open函数中的第二个参数)打开文件user.txtpw.txt ,每次都重新创建文件,而不是append模式,这将允许您将 append 数据写入文件。

Try:尝试:

outF = open('user.txt', 'a')
outf= open('pw.txt', 'a')

instead of:代替:

outF = open('user.txt', 'w')
outf= open('pw.txt', 'w')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM