How do I write to a Python subprocess' stdin?

后端 未结 3 1318
鱼传尺愫
鱼传尺愫 2020-11-22 15:50

I\'m trying to write a Python script that starts a subprocess, and writes to the subprocess stdin. I\'d also like to be able to determine an action to be taken if the subpro

3条回答
  •  情话喂你
    2020-11-22 16:14

    It might be better to use communicate:

    from subprocess import Popen, PIPE, STDOUT
    p = Popen(['myapp'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
    stdout_data = p.communicate(input='data_to_write')[0]
    

    "Better", because of this warning:

    Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

提交回复
热议问题