2010-03-20 65 views
1

在我的python腳本中,我需要在for循環中調用一個可執行文件,然後等待該可執行文件將結果寫入「output.xml」。循環一個可執行文件以從Python腳本中獲取結果

我該如何設法使用wait()&我怎麼知道我的一個可執行文件何時結束生成結果以獲得結果?如何關閉該進程並打開一個新進程來再次調用可執行文件並等待新的結果?

import subprocess 
    args = ("bin/bar") 
    popen = subprocess.Popen(args) 

我需要等待「bin/bar」的輸出生成「output.xml」,然後從那裏讀取它的內容。

for index, result in enumerate(results): 
     myModule.callSubProcess(index) 
     #this is where the problem is. 
     fileOutput = open("output.xml") 
     parseAndStoreInSQLiteFileOutput(index, file) 

回答

1

Popen.wait()將使腳本等待,直到過程結束。之後沒有必要殺死這個進程,因爲它已經退出了。

1

我認爲做的最簡單的方法是使用call

import subprocess 
retcode = subprocess.call('command', shell=True) 

它等待進程結束和返回碼賦給變量。 有關更詳細的描述,請參閱python文檔中的17.1.subprocess - convenience functions。希望能幫助到你。

相關問題