在`python`中正确使用带有`subprocess`的管道文件

时间:2017-10-13 11:31:44

标签: python subprocess pipe

我正在尝试使用subprocessmoses开源SMT中提取翻译。测试我的代码:我下载的示例文件可以找到here

我一直试图将用于翻译的各种命令行组合在一起。命令行(来自文档)是:

% echo 'das ist ein kleines haus' | moses -f phrase-model/moses.ini

我迄今为止使用的subprocess改编自文档here

import subprocess
step1 = subprocess.Popen(['echo','das ist ein kleines haus'],shell=True,stdout=subprocess.PIPE)
step2 = subprocess.Popen(['/bin/moses', '-f', '/phrase-model/moses.ini'],shell=True,stdin=step1.stdout, stdout=subprocess.PIPE,)
end_of_pipe = step2.stdout
print 'Results:'
for line in end_of_pipe:
    print '\t', line.strip()

我收到一个错误,因为它在正确执行bin之后无法识别step2中的输入文件。因此它表示没有指定配置文件',尽管它位于' -f'之后。参数。

在这种情况下,我一直在阅读有关执行外部命令的不同库,例如shsystem等,但subprocess似乎仍然是我的正确命令。需要,我只是不明白为什么它没有在我的命令中正确执行每个参数。

有什么想法吗? 此外,我正在使用python2.7,以防它产生影响。

### Progress ###

依赖下面的一些评论,我已将代码更新为,现在我收到以下错误:

step1 = subprocess.Popen(['echo','das ist ein kleines haus'],stdout=subprocess.PIPE)
step2 = subprocess.Popen(['/bin/moses', '-f', 'phrase-model/moses.ini'],shell=True,stdin=step1.stdout, stdout=subprocess.PIPE,)

'Exception: util/file.cc:68 in int util::OpenReadOrThrow(const char*) threw Errno Exception because -1 == (ret = open(name,00)). No such file or directory while opening lm/europarl.srilm.gz'

0 个答案:

没有答案