You are opening and closing the file in every iteration with the w
mode, which means the file gets truncated every iteration, which in turn means it will always contain only the last thing you wrote to it.
You may use the a
mode which appends to the file.
A better approach will be to to open the file once before the loop, and close it once after the loop.
The best practice is to use the with
context manager (google the term to find more info) which will handle the opening and closing of the file for you.
import itertools
import string
variations = itertools.permutations(string.printable, 1)
with open("Output.txt", "w") as f:
for v in variations:
f.write('{}\n'.format(''.join(v)))
Note that I added \n
in the end of each line since I assumed you want each permutation in a separate line.