python - How to pass the same answer to subprocess.Popen automatically? -


i want call yes "y" | foo bar, using subprocess.popen. use this:

yes = subprocess.popen(['yes', '""'], stdout=subprocess.pipe) proc = subprocess.popen(['foo', 'bar'], stdin=yes.stdout, stdout=subprocess.pipe) 

but, of course, won't work on windows. how can works on every platform?

to emulate shell pipeline yes "y" | foo bar in python:

#!/usr/bin/env python subprocess import popen, pipe  foo_proc = popen(['foo', 'bar'], stdin=pipe, stdout=pipe) yes_proc = popen(['yes', 'y'], stdout=foo_proc.stdin) foo_output = foo_proc.communicate()[0] yes_proc.wait() # avoid zombies 

to pipe input in python (without yes utility), use threads or async. i/o input_iterator itertools.repeat(b'y' + os.linesep.encode()).


Comments