Reputation: 171
I have the following code. I am trying to make the code save the print to the file at the end of the file. What part of this would I be missing?
import itertools
#Open test.txt
file = open('test.txt', 'a')
res = itertools.product('abcdef', repeat=3) # 3 is the length of your result.
for i in res:
print ''.join(i)
Upvotes: 0
Views: 75
Reputation: 4786
You basically didn't link your printing with writing to the file, as print
prints the output to the stdout
which by default is your screen, although it can be changed of course.
In my example, I used the with
statement, which means the file will be automatically closed once the code below it finishes to execute, hence you don't need to remember close the file when done processing.
For more information about with
you can read here
You can do something like:
import itertools
res = itertools.product('abcdef', repeat=3) # 3 is the length of your result.
with open('test.txt', 'a') as f_output:
for i in res:
f_output.write(''.join(i))
f_output.write('\n')
Upvotes: 3