我讀的每個線程我在計算器上發現使用subprocess
從Python中調用shell命令,但我找不到適用於我的情況如下答案子:殼牌管道可以用Python
我想這樣做來自Python的以下內容:
運行shell命令
command_1
。收集輸出在可變result_1
殼管
result_1
成command_2
並收集result_2
的輸出。換句話說,使用我運行當在步驟command_1
之前照此管道
result_1
到第三命令command_3
和result_3
收集結果而獲得的結果運行command_1 | command_2
。
到目前爲止,我曾嘗試:
p = subprocess.Popen(command_1, stdout=subprocess.PIPE, shell=True)
result_1 = p.stdout.read();
p = subprocess.Popen("echo " + result_1 + ' | ' +
command_2, stdout=subprocess.PIPE, shell=True)
result_2 = p.stdout.read();
的原因似乎是"echo " + result_1
不模擬獲得命令的輸出管道的過程。
這是所有可能的使用子進程?如果是這樣,怎麼樣?
請參閱[本文檔中的示例](http://docs.python.org/library/subprocess.html#replacing-shell-pipeline)以獲取正確的方法。 – 2012-03-03 01:00:34
謝謝@SvenMarnach,那還能讓我收集Python變量中第一條命令的輸出嗎? – 2012-03-03 01:03:52