【发布时间】:2017-08-29 15:05:24
【问题描述】:
如何告诉 python 逐行读取 txt 列表? 我正在使用 .readlines() 似乎不起作用。
import itertools
import string
def guess_password(real):
inFile = open('test.txt', 'r')
chars = inFile.readlines()
attempts = 0
for password_length in range(1, 9):
for guess in itertools.product(chars, repeat=password_length):
attempts += 1
guess = ''.join(guess)
if guess == real:
return input('password is {}. found in {} guesses.'.format(guess, attempts))
print(guess, attempts)
print(guess_password(input("Enter password")))
test.txt 文件如下所示:
1:password1
2:password2
3:password3
4:password4
目前该程序仅适用于列表中的最后一个密码 (password4) 如果输入任何其他密码,它将运行列表中的所有密码并返回“无”。
所以我假设我应该告诉 python 一次测试每一行?
PS。 “return input()”是一个输入,所以对话框不会自动关闭,没有什么可输入的。
【问题讨论】:
-
我有点担心您似乎存储纯文本密码。
-
@TomdeGeus 你的陈述绝对有效,但如果我猜的话,这可能是一个练习,而不是一个真正的应用程序。
标签: python list python-3.x brute-force