1、添加用户和密码到passwd文件中

cat  add_user.py

# 将几个用户密码写进passwd文件,用户登陆认证的时候读取此文件
f = open("passwd", "w+", encoding="utf-8")
f.write("zhangsan  123456\n")
f.write("qiaofeng  abc123\n")
f.write("shudihuang  a123b\n")
f.write("wangyuyan ab123c")
f.flush()
f.close()

 cat  User_login.py

# 将几个用户密码写进passwd文件,用户登陆认证的时候读取此文件
f = open("passwd", "r", encoding="utf-8")

#f.tell()
f.seek(0)  #刚写入文件,光标在最后,需要将光标移到0的位置才能读到上面写入的内容

username_input = input("username:")
f_lock = open("user_lock", "r+", encoding="utf-8")
for lock_line in f_lock:
    if username_input.strip() == lock_line.strip():
        print("{_username}".format(_username=username_input) + " is in the userlocklist.")
else:
    for line in f:
        username = line.split()
        if username_input.strip() == username[0]:
            for i in range(4):
                if i == 3:
                    print("{_username}'s password error more than 3 times".format(_username = username_input) + " was locked.")
                    f_lock.write(username_input + "\n")
                    f_lock.flush()
                    break
                password_input = input("password:")
                if password_input == username[1]:
                    print("Welcome to {_username}".format(_username = username_input) + "'s blog!!!")
                    break
                else:
                    print("Password error, please input again!")
                    continue
    # else:
    #     print("User {_userNotExist}".format(_userNotExist=username_input) + " is not exist.")

f_lock.close()
12-22 00:14