简体   繁体   English

我在 python 中使用 open() 命令时遇到问题

[英]i am having trouble with open() command in python

I am currently developing a desktop login-register app for my practice, and was having some trouble with login and register.我目前正在为我的实践开发桌面登录注册应用程序,并且在登录和注册时遇到了一些问题。 If I type REGISTER and add my info in f.write() command it stores my info but after that everything just goes, like the whole file gets formatted (this was register issue).如果我输入REGISTER并在f.write()命令中添加我的信息,它会存储我的信息,但之后一切都会进行,就像整个文件被格式化一样(这是注册问题)。

The login issue is if I want to check whether a name or password in file exists or not (this command could be wrong).登录问题是如果我想检查文件中的名称或密码是否存在(此命令可能是错误的)。 I tried to use if login_email and login password in f: but it says that login_email and password do not exist.我尝试if login_email and login password in f:使用if login_email and login password in f:但它说login_emailpassword不存在。

Code:代码:

f = open('pass.txt', 'w')
fr = open('pass.txt', 'r')
from time import sleep

login_list = "LOGIN"
register_list = "REGISTER"

if 1 > -3232:
    print("Type register for new account\ntype login for login into existing account")
    bi = input("==>    ")

    if bi.upper() in login_list:
        print("you are registered?? nice now loginnn!!")
        login_1 = input("your username:   ")
        login_2 = input("your password:   ")
        if login_1 and login_2 in fr:
            print("Nice my program worked??")
            exit()
        else:
            exit()

    elif bi.upper() in register_list:
        print("you are in register section: ")
        sleep(.9)
        print("NOTE:  Your password should only contain alphabets!")
        sleep(4)
        reg_1 = input("your username:     ")
        sleep(.9)
        reg_2 = input("your password:     ")
        sleep(.9)
        reg_2v1 = input("confirm password")
        if reg_2 == reg_2v1:
            f.write(reg_1 + " : " + reg_2 + "\n")
            print("now login again,\")
        else:
            print("invalid password, try again")

    else:
        print("you gave me the wrong command")

else:
    exit()
  1. You shouldn't open the file in both read and write mode at the beginning of the script.您不应在脚本开头以读写模式打开文件。 Opening it in write mode empties the file, so you won't be able to read it.以写入模式打开它会清空文件,因此您将无法读取它。 You'll also wipe out all the other usernames and passwords.您还将清除所有其他用户名和密码。 You should open the file in read mode when logging in, and append mode when registering, to add a new line without removing the old ones.登录时应以读取模式打开文件,注册时应以追加模式打开文件,以便在不删除旧行的情况下添加新行。 And you should use with to just open the file around the code that needs to use it.您应该使用with来打开需要使用它的代码周围的文件。
  2. if login_1 and login_2 in fr: is not the correct way to test if both the username and password are in the file. if login_1 and login_2 in fr:不是测试用户名和密码是否都在文件中的正确方法。 Due to operator precedence, that's parsed as if login_1 and (login_2 in fr): .由于运算符优先级,这被解析为if login_1 and (login_2 in fr): This just checks that login_1 is not empty, and then just checks if login_2 is in the file.这只是检查login_1不为空,然后只检查login_2是否在文件中。 The second test will never work, because the lines of the file all end with newline, but login_2 doesn't, so they'll never match.第二个测试永远不会成功,因为文件的所有login_2以换行符结尾,但login_2不会,所以它们永远不会匹配。

You need to check for the fully formatted line, including the newline.您需要检查完全格式化的行,包括换行符。

if f'{login_1} : {login_2}\n' in fr:
  1. if bi.upper() in login_list: seems suspicious. if bi.upper() in login_list:似乎可疑。 login_list is not a list, it's a string. login_list不是一个列表,它是一个字符串。 So this will check whether bi.upper() is any substring -- it will succeed if the user enters log or in or gi , not just login .因此,这将检查bi.upper()是否是任何子字符串——如果用户输入logingi ,而不仅仅是login ,它将成功。 Is that intentional?这是故意的吗?

Full code:完整代码:

from time import sleep

login_list = "LOGIN"
register_list = "REGISTER"

if 1 > -3232:
    print("Type register for new account\ntype login for login into existing account")
    bi = input("==>    ")

    if bi.upper() in login_list:
        print("you are registered?? nice now loginnn!!")
        login_1 = input("your username:   ")
        login_2 = input("your password:   ")
        with open('pass.txt', 'r') as fr:
            if f'{login_1} : {login_2}\n' in fr:
                print("Nice my program worked??")
                exit()
            else:
                exit()

    elif bi.upper() in register_list:
        print("you are in register section: ")
        sleep(.9)
        print("NOTE:  Your password should only contain alphabets!")
        sleep(4)
        reg_1 = input("your username:     ")
        sleep(.9)
        reg_2 = input("your password:     ")
        sleep(.9)
        reg_2v1 = input("confirm password")
        if reg_2 == reg_2v1:
            with open('pass.txt', 'a') as f:
                f.write(reg_1 + " : " + reg_2 + "\n")
            print("now login again,")
        else:
            print("invalid password, try again")

    else:
        print("you gave me the wrong command")

else:
    exit()

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

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