Jaffer Wilson Jaffer Wilson - 2 years ago 54
Python Question

Subprocess in python2 returns error

I am trying to input the text to the

ots
summerizer on Ubuntu. I am using the python2
subprocess
library. But consistently getting the following error:

text = "the shards that cut me the deepest were the ones that intended to cut , obama to melania trump as first lady 's largest public appearance since the 2016 election - speaking in front of more than 8,000 people at the women 's foundation of colorado 's 30th anniversary celebration - and she touched on personal attacks that she faced again and again"
>>> sum = subprocess.check_output('ots --ratio=30 <' + text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 567, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 36] File name too long


Even I have read that the
ots
takes input from the file or the
stdin
. Hence I tried to put input as:

sum = subprocess.check_output(['echo',text,'> stdin'])


But getting the following output:

"the shards that cut me the deepest were the ones that intended to cut , obama to melania trump as first lady 's largest public appearance since the 2016 election - speaking in front of more than 8,000 people at the women 's foundation of colorado 's 30th anniversary celebration - and she touched on personal attacks that she faced again and again > stdin\n"


But it is not what I am trying to achieve. I just want to input the text to the
ots
library using the
subprocess
of python. Is theer a way to do that and get the summary from the
ots
faster? Kindly, help me.

Answer Source

I believe you need the | pipe operator.

sum = subprocess.check_output('echo {} | ots --ratio=30'.format(text), shell=True)

The < symbol in bash is used take input from another stream.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download