redirect stdout to a file and terminate if stderr is encountered using python subprocess

Viewed 55

I am writing a pipeline in python 3.6 which internally executes several command-line based software. Each internal software is executed using subprocess.run() . I want to

  1. redirect the stdout of each software to a separate file,
  2. if there is an error produced by an internal software I want to redirect the error to stderr (so the user can see it), redirect to a separate file and terminate the execution of the pipeline.

The first step I do like this:

with open("outfile", "wb", 0) as outfile:
    subprocess.run(cmd, stdout=outfile, check=True)

My question 1 is if there is a better way of achieving the same? There are many commands in the pipeline and I don't want open separate file for each of them.

Regarding the second question, so I am not sure how to solve it: one way would be to write the stderr to a file and then check if the file is empty, but I feel its a wrong way of doing it.

Any suggestions are appreciated,

0 Answers
Related