Python:try-except,在应该的时候不调用try

时间:2015-04-13 14:56:31

标签: python zipfile try-except

我真的不知道为什么我的“try-except”即使应该也不会“尝试”。 它只是将“except”中的消息打印成正确的密码为“result”。 我已经尝试过一些东西但似乎没什么用。

(我不想用这个程序做违法的事。我只对itertools和zipfile模块感兴趣。)

代码:

import itertools, sys, zipfile

chars = input("Chars:" + " ")
max_length = input("Max length:" + " ")
zip_name = input("Zip name:" + " ")

max_length = int(max_length)

if chars in ("letters", "Letters"):
    chars = "abcdefghijklmnopqrstuvwxyz"
if chars in ("numbers", "Numbers"):
    chars = "0123456789"

print()

input("Press enter to start...")

print()

length = (max_length + 1) - max_length

zip_file = zipfile.ZipFile(zip_name)

while length <= max_length:
    results = itertools.product(chars, repeat = length)
    for result in results:
        result = "".join(result)
        try:
            zip_file.extractall(pwd = result)
            print()
            print("Password found:", result)
            print("Sucessfully extracted all files from", zip_name + ".")
            print()
            input("Press enter to exit...")
            sys.exit()
        except:
            print("Password not yet found:", result)
    length = length + 1

print()
print("Couldn't find the password.")

1 个答案:

答案 0 :(得分:2)

好吧,看起来你在这一行上有一个例外:

zip_file.extractall(pwd = result)

然后转到except块。

如何解决:删除try / except,看看输出中会出现什么。