0

I have an external (i.e., not changeable) program that takes three input values <a>, <b>, and <c> (prompted).

$ COMMAND
A: <a>
B: <b>
C: <c>

I'd like to call this program using subprocces.run like this.

result = subprocess.run(
    "COMMAND", shell=True, stdout=subprocess.PIPE).stdout.decode('utf-8').split('\n')

How can I pass those input values?

So far, I've tried to do things like this.

(a)

read, write = os.pipe()
os.write(write, "<a>\n")
os.write(write, "<b>\n")
os.write(write, "<c>\n")
os.close(write)

and

result = subprocess.run(
    "COMMAND", shell=True, stdout=subprocess.PIPE, input=read).stdout.decode('utf-8').split('\n')

(b)

result = subprocess.run(
    "COMMAND", shell=True, stdout=subprocess.PIPE, input="<a>\n<b>\n<c>\n", encoding=ascii).stdout.decode('utf-8').split('\n')

However, I can't get anything to work...

  • Rewrite `COMMAND` to optionally accept command line arguments and ask for input if the command line arguments are missing. – wwii Jul 08 '19 at 18:45
  • That's unfortunately not possible since it's an external program. – pystarter94 Jul 08 '19 at 18:49
  • you want it all in a single line? Related: [Read streaming input from subprocess.communicate()](https://stackoverflow.com/questions/2715847/read-streaming-input-from-subprocess-communicate) – wwii Jul 08 '19 at 19:01
  • It's three different prompts after each other. – pystarter94 Jul 08 '19 at 19:04
  • Looking for a dupe but it seems like you can read each *prompt* then *send* a response. – wwii Jul 08 '19 at 19:05
  • Not sure whether I understand. I do know the values `, , ` in advance. I just need to pass them. – pystarter94 Jul 08 '19 at 19:09
  • Possible duplicate of [Python subprocess and user interaction](https://stackoverflow.com/questions/14457303/python-subprocess-and-user-interaction) ... and [How to interact with python's subprocess as a continuous session](https://stackoverflow.com/questions/28903430/how-to-interact-with-pythons-subprocess-as-a-continuous-session) – wwii Jul 08 '19 at 19:28

0 Answers0