tee - Python - How do I split output? -
this question has answer here:
- how duplicate sys.stdout log file in python? 14 answers
newb python question. i've been reading on tee() , different ways of splitting output. cant find example splitting output terminal , log file. i've been playing around options , have far:
def logname(): env.warn_only = true timestamp = time.strftime("%d_%b_%y") return "%s_%s" % (env.host_string, timestamp) sys.stdout = open('/home/path/to/my/log/directory/%s' % logname(), 'w')
the above log file host name_datestamp won't display on screen. when want stop logging do:
sys.stdout = sys.__stdout__
how can log file definiton above , display terminal @ same time? on right path tee()?
like this?
[user@machine ~]$ python python 2.7.3 (default, aug 9 2012, 17:23:57) [gcc 4.7.1 20120720 (red hat 4.7.1-5)] on linux2 type "help", "copyright", "credits" or "license" more information. >>> import sys >>> >>> class tee(object): ... def __init__(self, logfile, stdio = sys.__stdout__): ... self.logf = open(logfile, 'w') ... self.stdio = stdio ... def write(self, data): ... self.logf.write(data) ... self.stdio.write(data) ... def flush(self): ... self.logf.flush() ... self.stdio.flush() ... def __getattr__(self, k): ... return getattr(self.stdio, k) ... def __dir__(self): ... return dir(self.stdio) ... >>> sys.stdout = tee('/tmp/output') >>> print 'some test output' test output >>> [user@machine ~]$ cat /tmp/output test output [user@machine ~]$
Comments
Post a Comment