Created
March 29, 2015 09:44
-
-
Save anonymous/87d07ffae48dc7abbe2e to your computer and use it in GitHub Desktop.
Tee with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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