0

I have some program (let's call it some_program) that prints to stdout, so when I run from my terminal the command below, a new file is been created (test.bin) with the content that was generated from the stdout of some_program:

some_program | tee test.bin

However, when I try to run some_program from a python script (python-3.6) and pipe it into tee to create the file test.bin, it doesn't work. I do see that some_program is running and done it's work, but test.bin wasn't created.

Here is my python script:

import subprocess
import os

CURR_DIR = os.getcwd()
OUTPUT_FILE = CURR_DIR + '/test.bin'

subprocess.check_output(['some_program', '|', 'tee', '{}'.format(OUTPUT_FILE)])

Any ideas?
Thanks

P.S.

I workaround it with this:

stdout = subprocess.run(['some_program'], stdout=subprocess.PIPE).stdout
f = open(OUTPUT_FILE, 'wb')
f.write(stdout)
f.close()

But I'm still curious why it doesn't work when piping with tee.

J.M.
  • 472
  • 1
  • 6
  • 15
  • You can't use shell commands with subprocess unless you set `shell=True` – mousetail Feb 07 '21 at 09:16
  • Indeed, you are passing `|` and `tee` as arguments to `someprogram` here. Apparently it simply ignores any arguments, or you would get an error message about that, like in the duplicate. Perhaps a better solution is simply to reimplement `tee` in Python, and keep the use of subprocesses to a minimum. – tripleee Feb 07 '21 at 09:19
  • Ok, now it's clear. Thanks guys – J.M. Feb 07 '21 at 09:23

0 Answers0