Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3密码检查器将只读取确切的txt文件内容_Python_Python 3.x - Fatal编程技术网

Python3密码检查器将只读取确切的txt文件内容

Python3密码检查器将只读取确切的txt文件内容,python,python-3.x,Python,Python 3.x,我的密码检查器正在工作,但是只有当用户输入与整个.txt文件相同时,它才会解析 如果其中任何密码与输入匹配,如何在.txt文件中输入多个密码并使程序工作?我希望能够添加密码123456,以便我的第二个if语句能够工作 secretpasswordfile.txt中只写入了genericpassword。假设文件中的每个密码都用换行符分隔,您可以检查其中是否有与此代码匹配的密码。它使用的事实是,您可以将open返回的文件对象视为文件中每一行的迭代器,并将键入的密码与每一行进行比较。.strip()

我的密码检查器正在工作,但是只有当用户输入与整个.txt文件相同时,它才会解析

如果其中任何密码与输入匹配,如何在.txt文件中输入多个密码并使程序工作?我希望能够添加密码123456,以便我的第二个if语句能够工作


secretpasswordfile.txt中只写入了genericpassword。

假设文件中的每个密码都用换行符分隔,您可以检查其中是否有与此代码匹配的密码。它使用的事实是,您可以将
open
返回的文件对象视为文件中每一行的迭代器,并将键入的密码与每一行进行比较。
.strip()
将从每行中拉出尾随的换行符

passwordfile = open('secretpasswordfile.txt')
print('Enter your password.')
typedpassword = input()
if any(typedpassword == pw.strip() for pw in passwordfile):
    print('Access granted.')
    if typedpassword == '123456':
        print('This password is not secure.')
else:
    print('Access denied.')

拥有纯文本密码。。。即使是到处乱搞。。。应该避免。尝试将密码文件加载到包含用户名和密码的字典中。我建议研究pbkdf2_sha256请,请,请不要以纯文本保存密码。看《谢天谢地》也许是值得的,这已经超出了我目前的专业水平,但随着我越来越熟练,我会仔细研究的。目前,工人社会保障号码列表只需保持纯文本形式;)
passwordfile = open('secretpasswordfile.txt')
print('Enter your password.')
typedpassword = input()
if any(typedpassword == pw.strip() for pw in passwordfile):
    print('Access granted.')
    if typedpassword == '123456':
        print('This password is not secure.')
else:
    print('Access denied.')