简体   繁体   中英

how to run an exe file with the arguments using python

Suppose I have a file RegressionSystem.exe . I want to execute this executable with a -config argument. The commandline should be like:

RegressionSystem.exe -config filename

I have tried like:

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])

but it didn't work.

You can also use subprocess.call() if you want. For example,

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)

The difference between call and Popen is basically that call is blocking while Popen is not, with Popen providing more general functionality. Usually call is fine for most purposes, it is essentially a convenient form of Popen . You can read more at this question .

For anyone else finding this you can now use subprocess.run() . Here is an example:

import subprocess
subprocess.run(["RegressionSystem.exe", "-config filename"])

The arguments can also be sent as a string instead, but you'll need to set shell=True . The official documentation can be found here .

os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")

应该管用。

Here i wanna offer a good example. In the following, I got the argument count of current program then append them in an array as argProgram = [] . Finally i called subprocess.call(argProgram) to pass them wholly and directly :

import subprocess
import sys

argProgram = []

if __name__ == "__main__":

    # Get arguments from input
    argCount = len(sys.argv)
    
    # Parse arguments
    for i in range(1, argCount):
        argProgram.append(sys.argv[i])

    # Finally run the prepared command
    subprocess.call(argProgram)

In this code i supposed to run an executable application named `Bit7z.exe" :

 python Bit7zt.py Bit7zt.exe -e 1.zip -o extract_folder

Notice : I used of for i in range(1, argCount): statement because i dont need the first argument.

I had not understood how arguments work. Ex: "-fps 30" are not one but two arguments which had to be passed like this (Py3)

args=[exe,"-fps","30"].

Maybe this helps someone.

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