The following command will output content from standard input to standard output, and will append the content into
three destinations - test1.txt
, test2.txt
and test3.txt
.
$ cat | python tee.py test{1,2,3}.txt --append
import argparse | |
import sys | |
from contextlib import ExitStack | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('filename', nargs='*') | |
parser.add_argument('--append', '-a', action='store_true') | |
args = parser.parse_args() | |
print(args) | |
with ExitStack() as stack: | |
files = [stack.enter_context( | |
open(fname, 'a' if args.append else 'w')) for fname in args.filename] | |
print(files) | |
for line in sys.stdin: | |
print(line.rstrip()) | |
for fh in files: | |
fh.write(line) |