2014-10-31 103 views
-2

我使用os.startfile('C:\\test\\sample.exe')來啓動應用程序。我不想知道應用程序的退出狀態,我只是想啓動exe。將參數傳遞給Python中的可執行文件

我需要的參數傳遞給EXE像'C:\\test\\sample.exe' -color

請建議在Python運行此方法。

+0

使用'os.system(「command」)' – Hackaholic 2014-10-31 17:53:51

回答

3

在我意識到的每種情況下,您應該使用subprocess模塊而不是os.startfileos.system

import subprocess 
subprocess.Popen([r'C:\test\sample.exe', '-color']) 

可能,如@Hackaholic暗示的意見,做

import os 
os.system(r'C:\test\sample.exe -color') 

但是,這不是簡單,the docs for os建議使用subprocess代替。

+0

子進程正在等待新的exe文件完成,但我的目標只是啓動新的exe文件。下面的代碼不起作用。 os.system(r'c:\\ Program Files \\ bin \\ winx64 \\ sample.exe -b -color') – Bala 2014-10-31 20:07:16

+0

@Bala Ah,對,忘了在新進程中打開os.startfile。我編輯了我的答案;如果你不想阻止呼叫,你應該使用'subprocess.Popen'。 – senshin 2014-10-31 20:11:06

+0

@Bala還要注意:你應該使用雙斜線''C:\\ foo'' _OR_原始字符串'r'C:\ foo''。不是都。如果你同時使用,你的文件路徑中會有太多的反斜槓。這可能是你評論中的'os.system'調用失敗的原因。 – senshin 2014-10-31 20:12:16

相關問題