多年前的这个问题满足了我的需要:
但是有没有办法使用 subprocess 模块来做到这一点?(我理解这是首选方式)
我查看了 stackoverflow、python 文档以及许多谷歌搜索,试图找到一种方法来使用标准输入将所需的输入发送到 p4 进程,但我没有成功。我已经能够在捕获子进程命令的输出方面找到很多东西,但无法理解输入命令。
一般来说,我对 python 很陌生,所以我可能会遗漏一些明显的东西,但我不知道在这种情况下我不知道什么。
这是我到目前为止提出的代码:
descr = "this is a test description"
tempIn = tempfile.TemporaryFile()
tempOut = tempfile.TemporaryFile()
p = subprocess.Popen(["p4","change","-i"],stdout=tempOut, stdin=tempIn)
tempIn.write("change: New\n")
tempIn.write("description: " + descr)
tempIn.close()
(out, err) = p.communicate()
print out