Python execute command line,sending input and reading output

前端 未结 1 2028
一向
一向 2020-12-09 13:21

How to achieve the following functionality:

  1. Python executes a shell command, which waits for the user to input something
  2. after the user t
相关标签:
1条回答
  • 2020-12-09 14:07

    You probably want subprocess.Popen. To communicate with the process, you'd use the communicate method.

    e.g.

    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)
    
    0 讨论(0)
提交回复
热议问题