在Python中向可执行文件传递参数
我正在使用 os.startfile('C:\\test\\sample.exe')
来启动一个应用程序。我不想知道这个应用程序的退出状态,只想简单地启动这个exe文件。
我需要给这个exe文件传递一个参数,比如 'C:\\test\\sample.exe' -color
。
请给我推荐一种在Python中运行这个的办法。
2 个回答
0
创建一个批处理文件 sam_ple.bat
,里面写上以下命令和参数
cd C:\test\
start sample.exe -color
然后把 sam_ple.bat
放在和你的 script.py 文件同一个文件夹里
在 Python 中输入以下代码来启动 exe 文件:
os.startfile('.\sam_ple.bat')
11
在我知道的所有情况下,你应该使用 subprocess
模块,而不是 os.startfile
或 os.system
。
import subprocess
subprocess.Popen([r'C:\test\sample.exe', '-color'])
你 可以,正如 @Hackaholic 在评论中提到的那样,做
import os
os.system(r'C:\test\sample.exe -color')
但是这样并没有更简单,而且 关于 os
的文档 推荐使用 subprocess
。