Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中正确使用管道文件和subprocess`_Python_Subprocess_Pipe - Fatal编程技术网

在python中正确使用管道文件和subprocess`

在python中正确使用管道文件和subprocess`,python,subprocess,pipe,Python,Subprocess,Pipe,我试图使用子流程从moses开源SMT获取翻译。要测试我的代码:可以找到我下载的示例文件: 我一直在尝试通过管道将用于翻译的各种命令行连接在一起。命令行(来自文档)是: 到目前为止,我一直在使用的子流程改编自文档: 我得到一个错误,在正确执行bin后,它无法识别step2中的输入文件。因此,它表示“未指定任何配置文件”,尽管它位于“-f”参数之后 在本例中,我一直在阅读有关执行外部命令的不同库的内容,例如sh,system,等等。但是子流程似乎仍然适合我的需要,我只是不明白为什么它不能正确执行命

我试图使用
子流程
moses
开源SMT获取翻译。要测试我的代码:可以找到我下载的示例文件:

我一直在尝试通过管道将用于翻译的各种命令行连接在一起。命令行(来自文档)是:

到目前为止,我一直在使用的
子流程
改编自文档:

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

在本例中,我一直在阅读有关执行外部命令的不同库的内容,例如
sh
system
,等等。但是
子流程
似乎仍然适合我的需要,我只是不明白为什么它不能正确执行命令中的每个参数

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

####进步####

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

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,)

int-util::OpenReadOrThrow(const char*)中的异常:util/file.cc:68没有引发异常,因为-1==(ret=open(name,00))。打开lm/europarl.srilm.gz'

err
'/phrase model/moses.ini'
时没有这样的文件或目录不是
'phrase-model/moses.ini'
。因此,“未指定配置文件”可能是一种笨拙的说法,表示“您指定的文件不存在”,在传递列表时不要使用
shell=True
;Python使用的是绝对路径。删除前导的
/
,或使用正确的绝对路径。除非继续使用
shell=True
导致新问题,否则问题在于使用了错误的工作目录。(这和
/bin/moses
可能需要更好的错误处理。)您可能不在您认为的目录中。使用
cwd
extra选项修复执行目录。err
'/phrase model/moses.ini'
不是
'phrase-model/moses.ini'
。因此,“未指定配置文件”可能是一种笨拙的说法,表示“您指定的文件不存在”,在传递列表时不要使用
shell=True
;Python使用的是绝对路径。删除前导的
/
,或使用正确的绝对路径。除非继续使用
shell=True
导致新问题,否则问题在于使用了错误的工作目录。(这和
/bin/moses
可能需要更好的错误处理。)您可能不在您认为的目录中。使用
cwd
extra选项修复执行目录。
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()
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,)