2014-01-19 71 views
0

這是我目前正在使用的代碼來嘗試獲取新行。可悲的是,它只是重寫了程序中的第一行。到底是怎麼回事?如果你能幫忙,我會非常感激。無法獲得新的工作在Python?

import itertools 
import string 
import sys, os, cmd 

from datetime import datetime 
FMT = '%Y-%m-%d %H:%M:%S' 
passwordstried = 0 


numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0',] 
symbols = ["!","@","#","$","%","^","&","*","(",")","_","-","'",'"',":",";","+","=","[","{","]","}","<",",",">",".","?","/","|","\"","~","`"] 
lowercaseletters = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","g","h","j","k","l","z","x","c","v","b","n","m"] 
uppercaseletters = ["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","G","H","J","K","L","Z","X","C","V","B","N","M"] 


stuff = lowercaseletters + uppercaseletters + numbers + symbols 


if (input("Do you have the length of the password?") == 'y'): 
    lengthstartingvalue = int(input("Password length: ")) 
else: 
    lengthstartingvalue = 1 


starttime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 
print(starttime) 


starttime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 
for L in range(lengthstartingvalue, len(stuff)+1): 
    for subset in itertools.combinations_with_replacement(stuff, L): 
     #print(subset) 


     password = ''.join(subset) 
     print(password) 

     file = open("generatedpasswords.txt","w") 
     file.write(str(password) + '\n') 

     passwordstried = passwordstried + 1 
    if (L>lengthstartingvalue+1): 
     break 

endtime = datetime.now().strftime('%Y-%m-%d %H:%M:%S') 
elapsed = datetime.strptime(endtime, FMT) - datetime.strptime(starttime, FMT) 
print ('Time elapsed:',elapsed) 
print ('Passwords tried:',passwordstried) 
file.close() 
+0

你必須要更多spec請發佈我們可以運行的代碼,以查看您遇到的問題。 – Christian

+0

那麼你發佈的代碼工作得很好....應該有一些其他的錯誤 – adil

+0

添加了所有的代碼。我似乎無法得到它的工作。 – Torkoal

回答

1

您有您重新打開循環內的同一個文件有問題...

file = open("generatedpasswords.txt","w") 
for L in range(lengthstartingvalue, len(stuff)+1): 
    for subset in itertools.combinations_with_replacement(stuff, L): 
     #print(subset) 


     password = ''.join(subset) 
     print(password) 


     file.write(str(password) + '\n') 




     passwordstried = passwordstried + 1 
    if (L>lengthstartingvalue+1): 
     break 

這將再次再次,然後重新打開相同的文件,並寫入最後的密碼文件最終
如果你想追加當前密碼,並且不想丟失以前的數據,那麼你可以在for循環中使用file = open("generatedpasswords.txt","a") ....

+0

這工作。非常感謝。 – Torkoal

+0

你可以接受答案,如果它的工作:) – adil

+0

我需要等待53秒。 :P – Torkoal