Question

I have created a certain function that is printing text through "print". i want to store that prompt to a text file.

the function name is "printall(x)".

I have tried the following

text_file = open('newfile.txt', 'w')
text_file.write(printall(x))
text_file.close

this did not work.

how can I make it happen?

Thanks

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top