使用Python运行带有命令行输入参数的.exe

时间:2016-03-11 10:51:03

标签: python c++

我有一个.exe,提示用户在命令行界面输入几个数字参数,然后在.txt中生成数据。我想使用Python,以便使用不同的数值参数重复运行.exe。

在Python中,我用:

调用了可执行文件
subprocess.call(["executable.exe"])

如何运行可执行文件并指定输入参数(注意:我不是指辅助参数,如-s,-t等,而是输入.exe的实际数字参数)?

由于

编辑:我的.exe是从.cpp创建的,当CLI提示时,用户给出的整数加倍。

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {
    int ExampleNumber;
    cout << "Please enter a number: ";
    cin >> ExampleNumber;
    ExampleNumber = ExampleNumber*2;

    ofstream ExampleFile;
    ExampleFile.open("ExampleFile.txt");
    ExampleFile << ExampleNumber;
    ExampleFile.close();
}

我尝试使用&#39; 3&#39;的输入运行.py。作为一个例子,它似乎仍然没有工作?

import subprocess

subprocess.call(["Executable.exe", '3'])

1 个答案:

答案 0 :(得分:0)

您可以按如下方式传递参数

subprocess.call(["executable.exe", '--parametername1', 'value1', 
 '--parameter2', 'value2'])

编辑:我没有给出代码时提到了这个答案。我假设该程序可以从CLI读取参数。我的答案只有在executable.exe可以使用来自命令行的输入参数时才有效。这不是这里的情况。