2017-08-01 117 views
1

我一直在研究Python腳本與ffmpeg進行交互;然而,我注意到,即使一切運行良好,stdout是空的,stderr返回我應該從stdout期望的東西。我如何解決它,以便輸出將由標準輸出返回?Python子流程返回輸出爲stderr

下面是再現現象最簡單的例子:

from subprocess import Popen, PIPE 

p = Popen(['python', '-V'], stdin=PIPE, stdout=PIPE, stderr=PIPE) 
out, err = p.communicate() 
if out: 
    print("Output. ", out.decode()) 
else: 
    print("Error. ", err.decode()) 

這裏的輸出:

Error. Python 3.6.1 :: Anaconda 4.4.0 (64-bit) 

我要指出,我使用的是Windows 10

+0

你在這裏有什麼問題? – Xatyrian

回答

2

您可以重定向stderr通過這樣做您的過程stdout

from subprocess import PIPE, STDOUT 
p = subprocess.Popen(["python", "-V"], stdout=PIPE, stderr=STDOUT) 

然後你就可以通過檢索的過程中,像這樣產生的輸出:

out = p.stdout.read() 

你的進程已經終止後,這將返回標準輸出的內容。