30

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.

1
  • 6
    It didn't work how? What's the error message? Commented Apr 10, 2013 at 14:44

5 Answers 5

31

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.

Sign up to request clarification or add additional context in comments.

1 Comment

28

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.

1 Comment

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.
3
os.system("/path/to/exe/RegressionSystem.exe -config "+str(config)+" filename")

Should work.

1 Comment

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.
1

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.