Home » Python » Python subprocess on Windows-Exceptionshub

Python subprocess on Windows-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment


I have a Windows executable that I am trying to run from within a python script on a Windows 2016 server. Basically, I do this:

import subprocess

myCmd = 'run.exe foo'
result = subprocess.run(myCmd) # tried it with & without shell=True

If I run this from a python or ipython shell, it works just fine. If I run it from my script, it does nothing, but it gives a return code of 0 (result -> CompletedProcess(args=’run.exe foo’, returncode=0).

Obviously I am doing something wrong :(. Can someone point me in the right direction?

How to&Answers:

are you sure you are in the right directory so it can find run.exe? you can try putting the full path to run.exe

EDIT: according to https://docs.python.org/3/library/subprocess.html#subprocess.run you need to run subprocess.run(myCmd.split(' ')), or subprocess.run([myCmd], shell=True) but the later might open you to shell injection vulnerabilities if you craft myCmd based on user input