2017-07-27 81 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' –