简体   繁体   中英

Handling a command line executable within in Python 2X via subprocess

I am wanting to run an executable that would normally be run directly on the command line but ultimately via a Python script. I used subprocess.Popen after reading through here and multiple Google results to achieve some limited success.

>>>import subprocess
>>>exe_path = sys.argv[1]
>>>dir_path_in = sys.argv[2]
>>>dir_path_out = sys.argv[3]
>>>subprocess.Popen([exe_path])

It then displays

<subprocess.Popen object at 0x021B7B30>

Followed by

>>>usage: <path to exe> [options] <dir_path> <dir_path_out>

But if I enter what you would normally expect to on the command line if used exclusively it returns:

>>>SyntaxError: invalid token

I have tested what is entered exclusively on the command line with the exe and it works fine just not via Python

I have had a look through StackOverFlow and the best kind of comparison I found was here How to handle an executable requiring interactive responses?

Ultimately the "usage" part will not even be required in the end as the declared sys.argvs will provide all the information the executable requires to run automatically.

The subprocess.call() achieved the desired result by declaring the argv variables and then concatenating the variables and using that final variable in a subprocess.call() as opposed to using shlex.split() which I first tried but it struggled with paths even with the '\\' escaped for Windows

import subprocess
exe_path = sys.argv[1]
dir_path_in = sys.argv[2]
dir_path_out = sys.argv[3]
command = exe_path, dir_path_in, dir_path_out
p = subprocess.call(command)

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