2014-10-31 105 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

相关问题