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...

7
  • Rewrite COMMAND to optionally accept command line arguments and ask for input if the command line arguments are missing. Commented Jul 8, 2019 at 18:45
  • That's unfortunately not possible since it's an external program. Commented Jul 8, 2019 at 18:49
  • you want it all in a single line? Related: Read streaming input from subprocess.communicate() Commented Jul 8, 2019 at 19:01
  • It's three different prompts after each other. Commented Jul 8, 2019 at 19:04
  • Looking for a dupe but it seems like you can read each prompt then send a response. Commented Jul 8, 2019 at 19:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.