FW: Printing to a file and a terminal at the same time
Lie Ryan
lie.1296 at gmail.com
Thu Oct 12 06:43:14 EDT 2017
It wouldn't be too difficult to write a file object wrapper that emulates tee, for instance (untested):
class tee(object):
def __init__(self, file_objs, autoflush=True):
self._files = file_objs
self._autoflush = autoflush
def write(self, buf):
for f in self._files:
f.write(buf)
if self._autoflush:
self.flush()
def flush(self):
for f in self._files:
f.flush()
use like so:
sys.stdout = tee([sys.stdout, open("myfile.txt", "a")])
Another approach is by bsandrow (https://github.com/bsandrow/pytee), which uses fork and os.pipe(), this more closely resembles what the actual tee command are actually doing.
More information about the Python-list
mailing list