简体   繁体   中英

Run .exe with command line input arguments with Python

I've got an .exe which prompts the user for input at the command line interface for several numerical parameters and then generates data in a .txt. I would like to use Python in order to run the .exe repeatedly with different numerical parameters.

In Python, I've called the executable with:

subprocess.call(["executable.exe"])

How can I run the executable and specify input parameters (note: I am not referring to miscellaneous parameters such as -s, -t, etc but actual numerical parameters which are fed into the .exe)?

Thanks

EDIT: My .exe was created from a .cpp which doubles a integer given by the user when prompted at 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();
}

I tried running the .py with the an input of '3' as an example but it does not seem to be working still?

import subprocess

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

You can pass arguments as follows

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

Edit: I mentioned this answer when the code was not given. I assumed that program can read parameters from CLI. My answer is valid only if executable.exe can use input arguments from command line which is not the case here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM