Skip to content

Instantly share code, notes, and snippets.

@tomer
Created April 28, 2021 19:37
Show Gist options
  • Save tomer/2a502c607e3c702580bf9236953c8ebe to your computer and use it in GitHub Desktop.
Save tomer/2a502c607e3c702580bf9236953c8ebe to your computer and use it in GitHub Desktop.
Simple `tee` alternative for operating systems without native tee command
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)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment