29

I need to execute this script from my Python script.

Is it possible? The script generate some outputs with some files being written. How do I access these files? I have tried with subprocess call function but without success.

fx@fx-ubuntu:~/Documents/projects/foo$ bin/bar -c somefile.xml -d text.txt -r aString -f anotherString >output

The application "bar" also references to some libraries, it also create the file "bar.xml" besides the output. How do I get access to these files? Just by using open()?

Thank you,

Edit:

The error from Python runtime is only this line.

$ python foo.py
bin/bar: bin/bar: cannot execute binary file
4
  • 1
    subprocess is what you need to use, can you provide an example so we have a better idea why it didn't work? Commented Mar 18, 2010 at 22:08
  • "subprocess call"? What is that? Please post the code you used and the error you actually got. Commented Mar 18, 2010 at 22:51
  • Yes, he's talking about the "call" function in the standard "subprocess" module, which is the better way to do this, although os.system may be adequate depending on his needs Commented Mar 19, 2010 at 1:37
  • Hi Kaleb, I edited the question. Commented Mar 19, 2010 at 12:04

4 Answers 4

47

For executing the external program, do this:

import subprocess
args = ("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.

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

2 Comments

Hi Peter, there is the error: bin/bar: bin/bar: cannot execute binary file and without any other information from the Python runtime. What is the cause?
it's about the executable's error. I have solved it, thanks Peter.
20

The simplest way is:

import os
cmd = '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.

2 Comments

Note that this has the same security caveat as subprocess.Popen(shell=True). Don't use it with anything other than a literal string.
I want to execute tesseract on every file in my folder, so I os.walk and then do the progr in the loop. How does it work?
9

For executing a unix executable file. I did the following in my Mac OSX and it worked for me:

import os
cmd = './darknet-classifier-predict-data/baby.jpg'
so = os.popen(cmd).read()
print(so)

Here print(so) outputs the result.

Comments

0

In Python 3.5 and higher1 you can use subprocess.run to do what Peter's answer does in less code:

import subprocess
args = ("bin/bar", "-c", "somefile.xml", "-d", "text.txt", "-r", "aString", "-f", "anotherString")
subprocess = subprocess.run(args, capture_output=True)
print(subprocess.stdout)

See this answer to understand the difference between the two functions.


1: For reading and saving the output of the command, the example above makes use of the capture_output=True flag, which was only added in Python 3.7.
In previous versions you will have to use subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) to achieve the same result.

1 Comment

It was added in 3.5, not 3.7.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.