2014-05-23 27 views

回答

0

您可以使用幾乎任何方法subprocess但需要連接管道。一個簡單的方法是在文檔:

>>> subprocess.check_output(
...  "ls non_existent_file; exit 0", 
...  stderr=subprocess.STDOUT, 
...  shell=True) 
'ls: non_existent_file: No such file or directory\n' 

該文檔還表示,以下有關subprocess.call

注意

不要使用標準輸出=管或標準錯誤= PIPE使用此功能因爲這可能會基於子進程輸出量而死鎖。當需要管道時,使用Popen和communications()方法。

0

我認爲你正在尋找stdout=subprocess.PIPE

0

正確的做法是創建一個Popen對象。例如:

from subprocess import Popen, PIPE 
proc = Popen('command arg --option', stdout=PIPE, stderr=PIPE, shell=True) 
return_code = proc.wait() 
stdout, stderr = proc.communicate() 
print(stdout if return_code==0 else stderr) 

您可以在Python docs中找到更多關於Popen物體的信息。

相關問題