python:提高child_exception,OSError:[Errno 2]没有这样的文件或目录 [英] python: raise child_exception, OSError: [Errno 2] No such file or directory
问题描述
我使用subprocess.popen()函数在python中执行命令,如下所示:
I execute a command in python using subprocess.popen() function like the following:
omp_cmd = 'cat %s | omp -h %s -u %s -w %s -p %s -X -' %(temp_xml, self.host_IP, self.username, self.password, self.port)
xmlResult = Popen(omp_cmd, stdout=PIPE, stderr=STDOUT)
在外壳中,它运行正常且没有错误,但是在python中,我得到了:
In the shell it runs fine without error, but in python I get:
File "/home/project/vrm/apps/audit/models.py", line 148, in sendOMP
xmlResult = Popen(omp_cmd, stdout=PIPE, stderr=STDOUT)
File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我搜索了错误,但没有一个解决了我的问题.有谁知道这个问题的原因是什么?谢谢.
I searched the error but none of them solved my problem. Does anyone know what's the cause of this issue? Thanks.
推荐答案
如果要将命令作为字符串传递给Popen
,并且如果命令中具有指向其他命令的管道,则需要使用shell=True
关键字.
If you're going to pass the command as a string to Popen
and if the commands have pipes to other commands in there, you need to use the shell=True
keyword.
我对omp
命令不是特别熟悉,但是它闻起来就像是无用的猫一样可怕.我认为实现此目标的更好方法是:
I'm not particularly familiar with the omp
command, but this smells an awful lot like a useless use of cat. I would think that a better way to achieve this would be to:
import shlex
omp_cmd = 'omp -h %s -u %s -w %s -p %s -X %s' %(self.host_IP, self.username, self.password, self.port, temp_xml)
xmlResult = Popen(shlex.split(omp_cmd), stdout=PIPE, stderr=STDOUT)
或者,如果这不是对cat的无用(您确实需要通过stdin来传送文件),也可以使用子进程来实现:
Or, if it's not a useless use of cat (You really do need to pipe the file in via stdin), you can do that with subprocess too:
import shlex
omp_cmd = 'omp -h %s -u %s -w %s -p %s -X -' %(self.host_IP, self.username, self.password)
with open(temp_xml) as stdin:
xmlResult = Popen(shlex.split(omp_cmd), stdin=stdin, stdout=PIPE, stderr=STDOUT)
这篇关于python:提高child_exception,OSError:[Errno 2]没有这样的文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!