简体   繁体   中英

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. It just prints the message from "except" with the correct password as "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.)

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.

How to solve: remove try/except and see what will appear in the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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