There seems to be a question/answer for all languages here except for python
(at least I haven't found it)
From this question Redirect stdout to a file in Python? I learned that in order to redirect all output from the screen to a file I just have to use:
import sys
some_list = ['elem1', 'elem2']
for elem in some_list:
sys.stdout = open(elem + '.log', 'w')
# Do lots of stuff that print messages.
print 'lots of stuff for', elem
print 'Code finished'
This stores all output to the elemx.log
files:
elem1.log
lots of stuff for elem1
elem2.log
lots of stuff for elem2
Code finished
I have two questions regarding this approach:
1- How can I make the last line printed line (ie: Code finished
) not be stored in the file created for the last elem
?
2- How can I have the stored output also show on screen?