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

Python: try-except, doesn't call try when it should

我真的不知道为什么我的 "try-except" 没有 运行 "try" 即使它应该。 它只是打印来自 "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.")

嗯,看起来你在这一行有一个例外:

zip_file.extractall(pwd = result)

然后进入 except 块。

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