2012-10-15 53 views
0
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) 

我該如何傳遞和使用python與cmd進行通信。如何使用python與命令行程序進行通信?

謝謝。 (使用Windows平臺)

+0

你爲什麼不在這裏粘貼接收器進程的相關部分? – Dhara

+0

可以嘗試類似'proc.stdin.write(data_to_write)' – avasal

+0

這是重複的。 –

回答

5

docs on communicate()解釋:與過程

交互:將數據發送至標準輸入。從stdout和 stderr中讀取數據,直到達到文件結束。等待進程終止。

communicate()塊一旦輸入已發送,直到程序完成執行。在你的例子中,程序在發送"1"後等待更多的輸入,但是Python在它到達下一行之前等待它退出,這意味着整個事件都會死鎖。

如果您想要互換地讀寫很多,請撥打make pipesstdin/stdout,然後向其中寫入/讀取。

+0

感謝它幫助我瞭解下一步該做什麼。 – user1717522

+1

@ user1717522 - 如果您發現問題的答案令人滿意,則應將其標記爲已接受。 – root

相關問題