Python executes the command line, sends input and output

How to achieve the following functions:

  • Python executes a shell command that waits for the user inputsomething
  • after user inputinput the program answers with someoutput
  • Python captures output
+5
source share
1 answer

You probably want to subprocess.Popen. To contact the process, you must use the method communicate.

eg.

process=subprocess.Popen(['command','--option','foo'],
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)
inputdata="This is the string I will send to the process"
stdoutdata,stderrdata=process.communicate(input=inputdata)
+7
source

All Articles