0

I have the following python code and just want to send a command to the terminal when it asks a particular question. Here is what I have so far

import subprocess
import sys
cmd = "Some application"
dat = str("")

p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE, 
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  • I thank this can help you. Go to >> [blocks - send input to python subprocess pipeline][1] [1]: http://stackoverflow.com/a/1616457/3992791 – zzzz zzzz Nov 29 '14 at 14:54

1 Answers1

0
p.communicate(input="this is my input to the subprocess")

If you need to trap the output, do this instead:

output = p.communicate(input="this is my input to the subprocess")[0]
rchang
  • 5,150
  • 1
  • 15
  • 25
  • Hmmm, to push enter at the end? I assumed it would be p.communicate(input="this is my input to the subprocess/n")[0] – user3577811 Nov 29 '14 at 13:16
  • Yes, you can explicitly include the newline in the input parameter if the application is expecting it (I did not make any assumptions about what you were calling with the subprocess) – rchang Nov 29 '14 at 13:18
  • The application is expecting the user to hit enter but it does not seem to like the code. The application asks "Do you want to continue? (n/Y)" but will not accept "Y/n" – user3577811 Nov 29 '14 at 13:38
  • I have placed a print before and after the function and it doesn't actually get past this point. Any help would be appreciated. – user3577811 Nov 29 '14 at 14:18
  • Not much to go on without knowing more about the application being opened by the subprocess, as well as the full body of your Python code that is invoking this subprocess. Perhaps it is expecting additional interactive input after the "Do you want to continue" question? – rchang Nov 29 '14 at 14:45