【发布时间】:2019-04-16 22:31:33
【问题描述】:
我想做的是,在 Python 中,以编程方式通过标准输入向进程发送一些初始命令,然后将输入传递给用户,让他们随后控制程序。 Python 程序应该简单地等待子进程由于用户输入而退出。从本质上讲,我想做的是:
import subprocess
p = subprocess.Popen(['cat'], stdin=subprocess.PIPE)
# Send initial commands.
p.stdin.write(b"three\ninitial\ncommands\n")
p.stdin.flush()
# Give over control to the user.
# …Although stdin can't simply be reassigned
# in post like this, it seems.
p.stdin = sys.stdin
# Wait for the subprocess to finish.
p.wait()
我如何将标准输入传回给用户(不使用raw_input,因为我需要用户的输入在每次按键时生效,而不是在按下回车后才生效)?
【问题讨论】:
标签: python subprocess pipe stdin