对于我正在做的任务,我需要破解锁定的PDF文件。我试图创建一个密码列表,但无法解决如何将此代码生成的结果输出到文本文件中。
from random import shuffle
with open('randomwords.txt', 'r') as data:
data = data.read().split()
while(True):
shuffle(data)
password = ''
for x in data[:3]:
password += x
print password.replace('o','0')
到目前为止我尝试过的所有内容都无法正常工作,如果有人能告诉我如何将此代码生成的结果输出到外部文本文件中,我们将不胜感激。
转换功能:
def transform(word):
from random import shuffle
with open('randomwords.txt', 'r') as data:
data = data.read().split()
while(True):
shuffle(data)
password = ''
for x in data[:3]:
password += x
print password.replace('o','0')
return word
答案 0 :(得分:0)
您可能需要打开另一个文件来编写输出。
from random import shuffle
def transform(word):
l = list(word)
shuffle(l)
new_word = ''.join(l)
return new_word[:3].replace('o', '0')
input_file = 'randomwords.txt'
output_file = 'passwords.txt'
with open(input_file, 'r') as word_file:
with open(output_file, 'wb') as pwd_file:
for r in word_file:
word = r.strip()
password = transform(word)
pwd_file.write(password + '\n')
参考:http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python