Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2015 09:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/87d07ffae48dc7abbe2e to your computer and use it in GitHub Desktop.
Save anonymous/87d07ffae48dc7abbe2e to your computer and use it in GitHub Desktop.
Tee with Python
import sys
class Tee(object):
def __init__(self, name, mode):
self.file = open(name, mode)
self.stdout = sys.stdout
sys.stdout = self
def __del__(self):
sys.stdout = self.stdout
self.file.close()
def write(self, data):
self.file.write(data)
self.stdout.write(data)
log1 = Tee('a.log', 'w')
print '123'
log1.__del__()
log2 = Tee('b.log', 'w')
print '456'
log2.__del__()
log3 = Tee('c.log', 'w')
print '789'
log3.__del__()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment