PDA

View Full Version : [python]logging output and displaying



Pyro.699
August 15th, 2008, 02:30 PM
Hello,

Im almost 100% sure that this can be done but how would you go about logging all the output (similar to going python file.py > file.txt) but also allowing the output to be displayed in the terminal.

Thanks
~Cody Woolaver

days_of_ruin
August 15th, 2008, 03:16 PM
outfile = open("log.txt,'w')
print >> outfile, "anystring"
print "anystring"

Pyro.699
August 15th, 2008, 03:21 PM
what about when using "sys.stdout.write"

days_of_ruin
August 15th, 2008, 03:23 PM
what about when using "sys.stdout.write"
Don't bother to use that.Thats why there is ">>".You will have to
find every print statement and just use two like I posted above.

deuce868
August 15th, 2008, 04:15 PM
I don't know about that, I mean why find/replace through your code when you can just redirect stdout and avoid all that?

http://www.faqs.org/docs/diveintopython/kgp_stdio.html

Definite thing you're better off remapping stdout at the top of the script and leave your prints alone.