0

I've added the following line to a python file in order to output all print strings to a file:

sys.stdout = open(logf, 'wb')

This does what I wanted, but also stops all print strings from appearing in the terminal. Is there a way to configure stdout to print to both destinations? (I'm trying to avoid writing a function for each use of print because there are many and it's a lot to rewrite. Also, I've perused several similar topics, but I haven't found a simple method.)

Xodarap777
  • 1,358
  • 4
  • 19
  • 42

1 Answers1

0
def fprint(output):
    print output
    with open("somefile.txt", "a") as f:
        f.write("{}\n".format(output))

This function would do it for you, use instead of print.

augre
  • 330
  • 3
  • 12
  • I saw this on SO from another question. I was just hoping there was another way to do this directly, or a way to alter the behavior of stdout. – Xodarap777 Jan 05 '15 at 00:19
  • This one would work perfectly too: http://stackoverflow.com/questions/14906764/how-to-redirect-stdout-to-file-and-console-with-scripting – augre Jan 05 '15 at 00:23