我正在尝试制作一个简单的密码管理器。这是我添加密码的函数:]
def add():
cont=input("You would like to add a password? (y/n)")
if cont.capitalize() == "Y":
passwords = open("Passwords.txt","a")
username_ = input("What is the username?")
password_ = input("What is the password?")
L = [username_, " ", password_]
if(passwords.writelines(L)):
print("Written successfully!")
print("Quiting to option select...\n")
main()
else:
print("Hmm. That didn't seem to work.")
main()
else:
print("Quiting to option select...\n")
main()
我总是收到“嗯。这似乎不起作用。” 当我尝试添加新密码时。其预期功能是,如果写入密码,则会显示写入成功。在这种情况下,“Written”意味着“附加”,因此文件开头的“a”。
我尝试使用它sudo
来运行它,但这不是问题。我尝试将附加更改为“w”。
您可以使用 try- except 方法。
另外,您可以尝试使用 check_output() 但我不知道 writelines 是否可以使用。
writeLines 没有任何返回值,因此在 if 语句中使用没有任何价值:)
https://python-reference.readthedocs.io/en/latest/docs/file/writelines.html