Search code examples
pythonsubprocesspopen

Pass input to Popen process


I cannot find a proper solution for the following case. So, basically I want to pipe two shell commands, and pass some input data (mypassword here) to the 2nd one. Here's some code demonstrating the problem:

import subprocess
import shlex

args1 = shlex.split("find /home/vasya/rmps/ -name *.rpm")
ps = subprocess.Popen(args1, stdout=subprocess.PIPE)

args2 = shlex.split("xargs rpmsign --addsign")
p2 = subprocess.Popen(args2, stdin=ps.stdout, stdout=subprocess.PIPE)
ps.stdout.close()
output = p2.communicate('mypassword\n')[0]
print(output)

When run it asks in the console: Enter pass phrase: and stops despite the fact that I am passing the pass phrase already (my mypassword input).

Where is my fault and how do I fix it?


Solution

  • Your stdin to xargs is the output of find, you can't also input something else.

    One option would be to write the result of find to a file and use the -a (--arg-file) option of xargs. This would allow you to input filenames while still keeping stdin free to be piped in from your python program (via communicate)