2014-10-11 31 views
3

我想從我的Groovy腳本執行python外部過程,但它不產生任何輸出。Groovy腳本無法執行外部過程

所以,作爲一個小的完整性測試我試圖簡單地輸出Python版本:

def command = """ /usr/local/bin/python -V """ 
    def proc = command.execute() 
    proc.waitFor() 
    println "This is output: " + proc?.in?.text 

以上不產生任何輸出,不過,從我的命令行我能夠運行/usr/local/bin/python -V

奇怪的是,如果我修改腳本運行identify,那麼它確實會產生一個輸出。

def command = """ /usr/local/bin/identify --version """ 
    def proc = command.execute() 
    proc.waitFor() 
    println "This is output: " + proc?.in?.text 

什麼能導致這種行爲?

回答

3

python -V命令將版本號打印到標準錯誤而不是標準輸出。

$ python -V 
Python 2.7.8 
$ python -V 2>/dev/null 
$ 

因此,要輸出,重定向標準錯誤(2)到stdout(1):

def command = """ /usr/local/bin/python -V 2>&1 """ 
def proc = command.execute() 
proc.waitFor() 
println "This is output: " + proc?.in?.text 

或者,你可以使用err代替in得到標準錯誤輸出:

def command = """ /usr/bin/python -V """ 
def proc = command.execute() 
proc.waitFor() 
println "This is output: " + proc?.err?.text 
+0

哇,永遠不會知道這一點。我的Python腳本輸出的東西使用'打印'的東西「'不工作,這就是爲什麼我嘗試'-V'。那是否會打印到標準輸出? – Anthony 2014-10-11 03:15:48

+0

'print'語句(在PYthon 3.x中的函數)會將消息打印到標準輸出,除非您使用>> >> sys.stderr'(或Python 3.x中的'file = sys.stderr') – falsetru 2014-10-11 03:16:47