假设customPassFile.txt中有两行。第一行是" 123测试"第二行是" testing321"。如果passwordCracking =" 123testing",那么输出将是" 123testing"在文件中找不到(或类似的东西)。如果passwordCracking =" testing321",那么输出将是" testing321"在文件中找到了。我认为我所拥有的for循环只是读取文本文件的最后一行。解决这个问题的任何解决方案?
import time
import linecache
def solution_one(passwordCracking):
print("Running Solution #1 @ " + time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
startingTimeSeconds = time.time()
currentLine = 1
attempt = 1
passwordFound = False
wordListFile = open("customPassFile.txt", encoding="utf8")
num_lines = sum(1 for line in open('customPassFile.txt'))
while(passwordFound == False):
for i, line in enumerate(wordListFile):
if(i == currentLine):
line = line
passwordChecking = line
if(passwordChecking == passwordCracking):
passwordFound = True
endingTimeSeconds = time.time()
overallTimeSeconds = endingTimeSeconds - startingTimeSeconds
print("~~~~~~~~~~~~~~~~~")
print("Password Found: {}".format(passwordChecking))
print("ATTEMPTS: {}".format(attempt))
print("TIME TO FIND: {} seconds".format(overallTimeSeconds))
wordListFile.close()
break
elif(currentLine == num_lines):
print("~~~~~~~~~~~~~~~~~")
print("Stopping Solution #1 @ " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print("REASON: Password could not be cracked")
print("ATTEMPTS: {}".format(attempt))
break
else:
attempt = attempt + 1
currentLine = currentLine + 1
continue
答案 0 :(得分:0)
**我会在OP了解缩进的问题后删除这个答案,因为我理解他的代码意图。*
ifelse
这里你的代码在for循环之外,所以只缓存了最后一行。
for i, line in enumerate(wordListFile):
if(i == currentLine):
line = line
passwordChecking = line
#rest of the code.