How to make a call to an executable from Python script?
For executing the external program, do this:
import subprocessargs = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")#Or just:#args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split()popen = subprocess.Popen(args, stdout=subprocess.PIPE)popen.wait()output = popen.stdout.read()print output
And yes, assuming your bin/bar
program wrote some other assorted files to disk, you can open them as normal with open("path/to/output/file.txt")
. Note that you don't need to rely on a subshell to redirect the output to a file on disk named "output" if you don't want to. I'm showing here how to directly read the output into your python program without going to disk in between.
The simplest way is:
import oscmd = 'bin/bar --option --otheroption'os.system(cmd) # returns the exit status
You access the files in the usual way, by using open()
.
If you need to do more complicated subprocess management then the subprocess module is the way to go.
For executing a unix executable file. I did the following in my Mac OSX and it worked for me:
import oscmd = './darknet classifier predict data/baby.jpg'so = os.popen(cmd).read()print so
Here print so
outputs the result.