0

I have shell script. Which has :

./dtapi get_probability decision_tree simulated_diabetes_incidence_data_new.txt  AGE 70 weight 34 height 5.5 sex 0 ds1 34

Now I am trying to execute this shell script withing python script and store the result into some variable. test.py contains -

import os, sys
result = os.system("sh cmd_dtapi.sh")
print "Result is : ", result

But it behaves like this:

python test.py 
{"risk_of_disease":"2.122e-314"}Result is :  0

Result get printed directly, assignment takes 0 .

How can I store the result into some variable?

Update

After following answer -

import subprocess
import json
result_process_output = subprocess.check_output("sh cmd_dtapi.sh")
result_json = json.loads(result_process_output)
result = result_json["risk_of_disease"]
print "Result is : ", result    

Gives

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    result_process_output = subprocess.check_output("sh cmd_dtapi.sh")
  File "/usr/lib/python2.7/subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
1

1 Answer 1

3

Here is despcription of os.system():

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. If command generates any output, it will be sent to the interpreter standard output stream.

Which means, that your shell prints {risk_of_disease":"2.122e-314"} to standard output.

As for os.system() returning 0:

On Windows, the return value is that returned by the system shell after running command. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

So return code of your shell script was 0 and it is assigned to result. So technically you are already storing result in variable.

@edit:

To resolve problem, you'd need to use subprocess module which allows for more detailed manipulation of system calls.

import subprocess
import json
result_process_output = subprocess.check_output("sh cmd_dtapi.sh", shell=True)
result_json = json.loads(result_process_output)
result = result_json["risk_of_disease"]
print "Result is : ", result
Sign up to request clarification or add additional context in comments.

5 Comments

it describes os.system but it doesn't resolve problem.
@furas Does this seem like proper resolution?
@TomaszPlaskota: I am using python 2.7.6. When I tried it, I get OSError: [Errno 2] No such file or directory full error trace in question
@user123 - you need shell=True - check_output("sh cmd_dtapi.sh", shell=True) or you have to split command to list ["sh", "cmd_dtapi.sh"] - check_output(["sh", "cmd_dtapi.sh"])
Thanks @furas . it helped

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.