2017-07-27 89 views
0

我试图在Ubuntu上输入文本到ots summerizer。我正在使用python2 subprocess库。但一直得到以下错误:python2中的子进程返回错误

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的subprocess将文本输入到ots库。这是否是一种方法,并从ots快速获得摘要?请帮助我。

+0

'''--ots'='<'+ text'使用'text'中的字符串作为文件名来读取。我不知道'ots',也许它有办法从命令行接受一个字符串。如果没有,您可以通过使用[here string](http://tldp.org/LDP/abs/html/x17837.html)将它作为命令行字符串传递,就好像它来自文件一样:''ots - -ratio = 30 <<<'+ text',但是你必须告诉'subprocess'使用'bash'而不是'sh',因为'sh'不支持这里的字符串。 –

回答

0

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

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

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

+0

收到以下错误:'回溯(最近通话最后一个): 文件 “”,1号线,在 文件 “/usr/lib/python2.7/subprocess.py”,线路567,在check_output 过程= Popen(stdout = PIPE,* popenargs,** kwargs) 文件“/usr/lib/python2.7/subprocess.py”,第711行,在__init__中 errread,errwrite) 文件“/ usr/lib/python2 .7/subprocess.py“,1343行,在_execute_child raise child_exception OSError:[Errno 2]没有这样的文件或目录 ' –

+1

@JafferWilson现在试试吗?我想我错过了'shell = True' –