First, do: import sys
at the top of your code.
Then, in your printall(x)
method, you should add the following as your first line:
sys.stdout = open('newfile.txt', 'w')
Then, whenever you do print, it will write the code to the file instead of the console.
Hope that helps.