假设我有一个文件RegressionSystem.exe
。我想用-config
参数执行这个可执行文件。命令行应该像:
RegressionSystem.exe -config filename
我尝试过:
regression_exe_path = os.path.join(get_path_for_regression,'Debug','RegressionSystem.exe')
config = os.path.join(get_path_for_regression,'config.ini')
subprocess.Popen(args=[regression_exe_path,'-config', config])
但它不起作用。
如果需要,您也可以使用subprocess.call()
。例如,
import subprocess
FNULL = open(os.devnull, 'w') #use this if you want to suppress output to stdout from the subprocess
filename = "my_file.dat"
args = "RegressionSystem.exe -config " + filename
subprocess.call(args, stdout=FNULL, stderr=FNULL, shell=False)
call
和Popen
之间的差异基本上是call
阻塞而Popen
不是,Popen
提供更多的一般功能。通常call
适用于大多数用途,它基本上是一种方便的Popen
形式。您可以在this question阅读更多内容。
os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")
应该管用。
接受的答案已过时。对于其他任何发现这个的人,你现在可以使用subprocess.run()
。这是一个例子:
import subprocess
subprocess.run(["RegressionSystem.exe", "-config filename"])
参数也可以作为字符串发送,但您需要设置shell=True
。官方文档可以找到here。