bappa147
bappa147

Reputation: 539

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.

Upvotes: 30

Views: 136994

Answers (5)

Daniel Butler
Daniel Butler

Reputation: 3726

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.

Upvotes: 27

Pit_87
Pit_87

Reputation: 11

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.

Upvotes: 1

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.

Upvotes: 0

Rested
Rested

Reputation: 197

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

Should work.

Upvotes: 3

hjweide
hjweide

Reputation: 12523

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.

Upvotes: 31

Related Questions