Skip to content

Instantly share code, notes, and snippets.

@yoshikakbudto
Last active December 28, 2018 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshikakbudto/bb3d091e04d5daa2374102adce0d7ebd to your computer and use it in GitHub Desktop.
Save yoshikakbudto/bb3d091e04d5daa2374102adce0d7ebd to your computer and use it in GitHub Desktop.
python feed STDIN to Popen process
#!/usr/bin/env python
from subprocess import Popen, PIPE
import shlex
items = ['i1', 'i2', 'i3']
cmd='xargs -rn1 -I% echo "run for %"'
p = Popen(shlex.split(cmd), stdout=PIPE, stdin=PIPE, stderr=PIPE)
# this will emulate the shell sequence: "echo 'i1 i2 i3'|tr ' ' '\n'|xargs ..."
(p_stdout, p_stderr) = p.communicate(input=b'\n'.join(items))
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment