28

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bappa147
  • 519
  • 3
  • 8
  • 18

5 Answers5

30

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.

Community
  • 1
  • 1
hjweide
  • 11,893
  • 9
  • 45
  • 49
24

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.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
  • And again, the documentation is crap. Do we really need to pass `"-config filename"` or is it `"-config", "filename"`? Calling the executable an argument does not really make sense either. – Thomas Weller Feb 20 '23 at 08:20
3
os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")

Should work.

Rested
  • 187
  • 2
  • 9
  • os.system appears to block. That is, not return until the exe has run to completion. That is a noteworthy feature/constraint of this propose solution. – Bernd Wechner Sep 17 '19 at 05:07
0

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.

0

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.

Pit_87
  • 1