简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM