python2中的子进程返回错误

时间:2017-07-27 09:20:56

标签: python python-2.7 subprocess libots

我正在尝试将文本输入到Ubuntu上的ots summerizer。我正在使用python2 subprocess库。但一直得到以下错误:

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

即使我已经读过ots从文件或stdin获取输入。因此我尝试将输入设为:

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

但是得到以下输出:

"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"

但这不是我想要实现的目标。我只想使用python的ots将文本输入到subprocess库。您是否可以更快地从ots获取摘要?请帮助我。

1 个答案:

答案 0 :(得分:0)

我相信您需要|管道运营商。

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

使用bash中的<符号从另一个流中获取输入。