ulfsigvardsson
ulfsigvardsson

Reputation: 33

How to properly pass information between subprocesses using pipes?

I'm trying to get a simple nested pipe working via a Python script using subprocesses, but the output I'm getting is not making sense.

I've tried to redirect the output of diff to grep and from grep to wc, and then checking the output but without luck.

import subprocess

diff = subprocess.Popen(("diff", "-y", "--suppress-common-lines", "file1.py", "file2.py"), stdout=subprocess.PIPE)
diff.wait()
grep = subprocess.Popen(("grep", "'^'"), stdin=diff.stdout, stdout=subprocess.PIPE)
grep.wait()
output = subprocess.check_output(('wc', '-l'), stdin=grep.stdout)
print(output)

I would like this to result in the number of rows that differs between file1.pyand file2.py, but instead I'm getting

b'       0\n'

From the command line, when I run diff -y --suppress-common-lines file1.py file2.py | grep '^' | wc -l it returns an integer.

Upvotes: 0

Views: 123

Answers (1)

adrtam
adrtam

Reputation: 7231

If you do in python subprocess call

("grep", "'^'")

In command line, you mean:

grep "'^'"

so the argument to grep is a 3-character string. If you do not mean that, simply do

("grep", "^")

Likely you will solve your problem.

PS: Similarly, do not expect any shell escape, variable substitution, etc. work in the argument to subprocess.Popen(). Those are shell functionalities, and the shell will massage them before passing on to the executable. So now you have to massage your own.

Upvotes: 1

Related Questions