Calling an external program from python

So, I have this shell script:

echo "Enter text to be classified, hit return to run classification."
read text

if [ `echo "$text" | sed -r 's/ +/ /g' | bin/stupidfilter data/c_rbf` = "1.000000" ]
 then
  echo "Text is not likely to be stupid."
fi

if [ `echo "$text" | sed -r 's/ +/ /g' | bin/stupidfilter data/c_rbf` = "0.000000" ]
 then
  echo "Text is likely to be stupid."
fi

I would like to write it in python. How to do it?

(As you can see, it uses the library http://stupidfilter.org/stupidfilter-0.2-1.tar.gz )

+3
source share
2 answers

To do this just like a shell script does:

import subprocess

text = raw_input("Enter text to be classified: ")
p1 = subprocess.Popen('bin/stupidfilter', 'data/c_trbf')
stupid = float(p1.communicate(text)[0])

if stupid:
    print "Text is likely to be stupid"
else:
    print "Text is not likely to be stupid"
+8
source

You can clearly execute commands as sub-shells and read the return value, as in a shell script, and then process the result in Python.

This is easier than loading C. functions.

stupidfilter, , - . , , - Python C.

- , .

+1

Source: https://habr.com/ru/post/1711043/


All Articles