简体   繁体   English

Python:try-except,在应有的情况下不调用try

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

I really don't know why my "try-except" doesn't run "try" even when it should. 我真的不知道为什么我的“ try-except”即使在应该运行时也不会运行“ try”。 It just prints the message from "except" with the correct password as "result". 它只是将来自“ except”的消息打印为正确的密码作为“ result”。 I already tried a few things but nothing seems to work. 我已经尝试了一些方法,但是似乎没有任何效果。

(I don't want to do something illegal with this programm. Im just interested in the itertools and zipfile module.) (我不想对该程序进行非法操作。我只是对itertools和zipfile模块感兴趣。)

Code: 码:

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.")

Well, it looks like you have an exception on this line: 好吧,看来您在这一行有一个例外:

zip_file.extractall(pwd = result)

Then it goes to the except block. 然后转到except块。

How to solve: remove try/except and see what will appear in the output. 解决方法:删除try / except并查看输出中将出现什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM