我有一个写入stdout并从stdin读取的进程。我想在Python中使用该进程从其stdout / stdin读取和写入。
我试过了:
process = Popen(['the_program_name'], stdout=PIPE, stderr=PIPE, stdin=PIPE)
然后我尝试使用
process.stdout.readline()
和
process.stdin.write('input to the_program_name')
但这不起作用......
任何想法如何做到这一点?
编辑:
在write语句之后,程序似乎阻塞了readline语句。如果我使用process.communicate然后我得到输出,但外部进程立即终止。
答案 0 :(得分:0)
阅读here:
相关代码:
import subprocess
print '\npopen2:'
proc = subprocess.Popen(['cat', '-'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
stdout_value = proc.communicate('through stdin to stdout')[0]
print '\tpass through:', repr(stdout_value)