Writing to stdout and a log file
Jeff Epler
jepler at unpythonic.net
Tue Apr 19 20:32:06 EDT 2005
This variation works:
#------------------------------------------------------------------------
class Tee:
def __init__(self, *args):
self.files = args
def write(self, data):
for f in self.files:
result = f.write(data)
return result
def writelines(self, seq):
for i in seq: self.write(i)
import sys
sys.stdout = Tee(sys.stdout, open("/tmp/stdout.log", "w"))
print 'STDOUT', sys.stdout
#------------------------------------------------------------------------
It appears that the 'print' statement always uses file.write if
isinstance(sys.stdout, file). I don't know whether this has been
reported as a bug before, or if there's a reason for the current
behavior. It may be an accidental behavior that is left over from the
days when builtin types were not subclassable.
Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050419/20e69174/attachment.sig>
More information about the Python-list
mailing list