我使用子進程在Python腳本內調用外部程序。外部程序產生大量輸出。我需要捕獲這個程序的輸出。當前的代碼是這樣的:使用通信通過子進程捕獲輸出無w/o
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write('gams "indus89.gms"\r\n')
while process.poll() != None:
line = process.stdout.readline()
print line
我與此代碼得到的錯誤是
過程試圖寫入到不存在的管道。
如果我使用以下代碼:
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write('gams "indus89.gms"\r\n')
o, e = process.communicate()
print o
然後該程序的輸出不捕獲。
我應該如何更改我的代碼,以便在運行時捕獲第三方程序的輸出?
爲什麼你使用'cmd.exe'和'shell = False',爲什麼不'''gams「indus89.gms」'''shell = True'? –