I have a function that does a lot of prints. I want these to go to stdout as well as to a file, so I currently have to duplicate every print: once for stdout and once for the file.
Is there a way I can print to something (not sure what!) that I can return from the function and simply print with two print statements in my calling script?
So instead of:
def func():
   print("1")
   print("1",file=out.txt)
   print("2")
   print("2",file=out.txt)
   print("3")
   print("3",file=out.txt)
   return()
I want something like:
def func2():
   print("1",  ???) ->string.out
   print("2",  ???) ->string.out
   print("3",  ???) ->string.out
   (or some such?)
   return(string.out)
func2()
print(string.out)
print(string.out,file=out.txt)
I know I can do it in a crude fashion by appending all my output to a string but this would involve lots of formatting that "print" takes care of (eg. with template).
Or maybe that's the answer - I just can't see how to do it in a neat pythonic fashion.