I am compiling a command to be executed on a unix system from python, containing multiple piping step. e.g.:
grep foo myfile | cmd1 | cmd2 > output
These correspond to a series of transformations on entries that have foo
in them from myfile
. I sometimes construct it as a command to be executed by os.system
and in other cases using the subprocess
module.
How can I do error checking on each part of the pipe, from Python? For example in 99% of cases, there are foo
entries in myfile
and the remaining pipe works. But if for some reason myfile
exists but contains no foo
entries, then the rest of the pipe breaks because you are piping empty files, and the remaining commands require non-empty inputs to work. This makes it hard to debug because all you see is the output of a broken pipe, not knowing which intermediate step failed.
Is there a way to construct pipes in Python that error check intermediate steps? E.g. check that the grep
portion of the pipe really contains some output, that the cmd1
output on grep
's input also contained some output, and so on? thanks.