0 python windows subprocess cmd communicate
import subprocess
import sys
proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE) #the cmd program opens
proc.communicate(input="filename.txt") #here the filename should be entered (runs)
#then the program asks to enter a number:
proc.communicate(input="1") #(the cmd stops here and nothing is passed)
proc.communicate(input="2") # (same not passing anything)
Run Code Online (Sandbox Code Playgroud)
我如何使用python传递和与cmd通信.
谢谢.(使用Windows平台)
在对文档communicate()解释了这一点:
与流程交互:将数据发送到stdin.从stdout和stderr读取数据,直到达到文件结尾.等待进程终止.
communicate()一旦输入被发送,块就会阻塞,直到程序完成执行.在您的示例中,程序在发送后等待更多输入"1",但Python在它到达下一行之前等待它退出,这意味着整个事情就会死锁.
如果您想要交替地进行大量读写操作,请将管道设置为stdin/ stdout和写入/读取它们.