0

I am trying to use Python to run an executable (Windows 7) with parameters. I have been able to make the program run, but the amount of parameters I can use that will prove the Python script worked with parameters is limited. The best one is formatted like so:

-debugoutput debug.txt

I have tested this using a windows shortcut with an edited target and it works, it creates a debug output in the program directory.

Here is the code I am using:

import subprocess
args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput debug.txt"]
subprocess.call(args)

This does run the program, but the debug output is not created. I have tried putting an "r" in front of the parameter but this made no difference. I assume it is a simple formatting error but I can't find any examples to learn from that are doing the same thing.

UPDATE:

Thanks for the answers everyone, all the same, simple formatting error indeed.

3
  • 2
    there can be amiguity due to the space, have you tried args = ['"yourpath.exe", "-debugoutput", "debug.txt"] ? Commented May 11, 2016 at 12:32
  • I do not know if it works, but import subprocess args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"] subprocess.run(args) Commented May 11, 2016 at 12:32
  • Splitting up the parameter does work, I don't know why I didn't think of trying that. Commented May 11, 2016 at 12:39

3 Answers 3

1

In-code definition results in invocation of shell command line:

C:\Users\MyName\LevelEditor\LevelEditor.exe "-debugoutput debug.txt"

As you can see, by merging -debugoutput debug.txt to single list element, you explicitly stated that space between them shouldn't be parsed as command line argument separator.

To achieve expected behavior put file name string as separate element to argument list.

[r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"]
Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know you need to split the arguments by the space, so your args would look like:

args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"]

Does that work?

1 Comment

Thanks, that worked. The output is appearing in the wrong folder (I had to search for it), but it is running the program with the parameter.
0

I do not know if it works, but

import subprocess
args = [r"C:\Users\MyName\LevelEditor\LevelEditor.exe", "-debugoutput", "debug.txt"]
subprocess.run(args)

Following the docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.