2015-07-21 68 views
-1

我一直在試圖製作一個簡單的zip文件密碼破解程序(只是爲了好玩,不是惡意的目的),但是我的try和except語句不起作用。無論輸入總是導致除語句,永遠不會執行的其他內容(即使該zip文件不提取物)如何捕獲我期望的特定異常?

import zipfile 

k = 0 
file = zipfile.ZipFile('john.zip') 
def check(i): 
    p = bytes(i, 'ascii') 
    try: 
     file.extractall(pwd=p) 
    except: 
     return False 
    else: 
     return True 

def crack(): 
     x = open('john(1).txt', 'r') 
     for i in x.readlines(): 
      i.strip('\n') 
      k = check(i) 
      if k == True: 
       print('Password is: ' + k) 
       break; 
      x.close() 
     x.close()` 
+3

你有沒有嘗試採取'try'塊的語句,所以你可以看到實際的錯誤? – TigerhawkT3

+0

是的,我已經自己運行代碼,並得到你所期望的錯誤,並且當輸入正確的密碼時沒有錯誤 – Ellis

+1

您是否嘗試過只捕獲異常'except Exception:'而不是'except:'? – johnharris85

回答

1

1)登錄錯誤的except塊。幫助很多。

2)您正在'for'循環中關閉文件。循環讀取文件中的行時出現錯誤的想法。

3)的最後一行在其端部的反向引號字符(可能是問題的錯字):`

0

我得到它與一對夫婦中裂紋()的變化的工作如下面的評論。這是什麼爲我工作:

… 
def crack(): 
    x = open('john(1).txt', 'r') 
    for i in x.readlines(): 
     i = i.strip() # not just the statement i.strip('\n') 
     k = check(i) 
     if k == True: 
      print('Password is: ' + i) # not print('Password is: ' + k) 
…