Search code examples
pythonpasswordsoutputcracking

Writing all outputs to a file (Python)


I have this code that should generate all possible combinations of digits and store them in a text file called Passwords4.txt. The issue here is that when I go to the text file it just shows 9999 instead of showing the numbers from 0000 to 9999.

import itertools
lst = itertools.product('0123456789', repeat=4) #Last part is equal to the password lenght
for i in lst:
    print ''.join(i)
f = open('Passwords4.txt', 'w')
f.write(str(''.join(i)) +'\n')
f.close()

Can someone explain what should I do?


Solution

  • Your f.write is not inside the loop, so it only happens once.

    You probably want the open() before the loop, and your f.write in the loop (indented, same as print).