Python登录系统读取txt账号密码触发ValueError解包错误求助
Python登录系统txt文件读取拆包报错排查
问题复现
编写基于Python的账号登录系统时,注册逻辑可正常将邮箱、SHA256加密后的密码写入database.txt文件,但登录环节执行到读取存储凭证的代码行时,抛出如下异常:ValueError: too many values to unpack (expected 2)
原实现完整代码如下:
import hashlib def signup(): email = input("Enter an email address: ") pwd = input("Enter a password: ") conf_pwd = input("Confirm your password: ") if conf_pwd == pwd: enc = conf_pwd.encode() hash1 = hashlib.sha256(enc).hexdigest() with open(r'D:\Python Programs\Login\database.txt', "a") as f: f.write(email + "\n") f.write(hash1 + "\n") f.close() print("You have registered successfully!") else: print("Password is not same as above! \nPlease try again") signup() def login(): email = input("Enter email: ") pwd = input("Enter password: ") auth = pwd.encode() auth_hash = hashlib.sha256(auth).hexdigest() with open(r'D:\Python Programs\Login\database.txt', "r") as f: stored_email, stored_pwd = f.read().split("\n") f.close() if email == stored_email and auth_hash == stored_pwd: print("Logged in Successfully!") else: print("Login failed! \nPlease try again") login() def welcome(): print("Welcome to the [Insert Game Name Here]") HaveAccount = input("Have you made an account before? (Y/N): ") if HaveAccount == ("Y"): login() elif HaveAccount == ("N"): signup() else: print("Please enter either 'Y' or 'N'") welcome() welcome()
报错原因
- 注册逻辑用追加模式
"a"写入文件,每注册1个账号就会往txt里新增2行内容:第一行是邮箱,第二行是密码哈希。只要注册账号数≥2,文件内的总行数就会超过2行。 - 登录逻辑里
f.read().split("\n")会把整个文件的所有内容按换行符切割成列表,比如2个账号就会切割出至少4个有效元素+1个文件末尾空字符串,总长度远大于2,此时强行把列表拆包赋值给stored_email, stored_pwd两个变量,就会触发值过多的拆包错误。 - 额外代码隐患:使用
with open()上下文管理器操作文件时,会自动完成文件关闭,不需要手动调用f.close();用递归调用login()/signup()处理输入错误、登录失败的重试逻辑,多次重试后会触发栈溢出,改用循环实现更稳妥。
修复方案
调整登录时的文件读取逻辑,逐行遍历存储的所有账号凭证做匹配,同时修正冗余代码和递归问题,补充重复注册、数据库文件不存在的边界判断,修复后代码如下:
import hashlib import os DB_PATH = r'D:\Python Programs\Login\database.txt' def signup(): while True: email = input("Enter an email address: ") pwd = input("Enter a password: ") conf_pwd = input("Confirm your password: ") if conf_pwd != pwd: print("Password is not same as above! \nPlease try again") continue enc = conf_pwd.encode() hash1 = hashlib.sha256(enc).hexdigest() # 检查邮箱是否已注册 is_exist = False if os.path.exists(DB_PATH): with open(DB_PATH, "r") as f: lines = [line.strip() for line in f if line.strip()] for i in range(0, len(lines), 2): if lines[i] == email: is_exist = True break if is_exist: print("This email has been registered! Please try another or login.") return with open(DB_PATH, "a") as f: f.write(email + "\n") f.write(hash1 + "\n") print("You have registered successfully!") return def login(): while True: email = input("Enter email: ") pwd = input("Enter password: ") auth = pwd.encode() auth_hash = hashlib.sha256(auth).hexdigest() login_success = False if not os.path.exists(DB_PATH): print("No account found! Please sign up first.") return with open(DB_PATH, "r") as f: lines = [line.strip() for line in f if line.strip()] # 每两行一组匹配账号密码 for i in range(0, len(lines), 2): stored_email = lines[i] stored_pwd = lines[i+1] if email == stored_email and auth_hash == stored_pwd: login_success = True break if login_success: print("Logged in Successfully!") return print("Login failed! \nPlease try again") def welcome(): print("Welcome to the [Insert Game Name Here]") while True: have_account = input("Have you made an account before? (Y/N): ").strip() if have_account == "Y": login() break elif have_account == "N": signup() break else: print("Please enter either 'Y' or 'N'") welcome()
内容的提问来源于stack exchange,提问作者Iron_Ryan19
相关产品推荐
相关产品推荐

