Python - 如何使用管道执行 shell 命令,但不使用“shell=True”?
我有一个情况想要在 Python 中执行以下 shell 命令并获取输出,
echo This_is_a_testing | grep -c test
我可以使用此 python 代码在 python 中执行上述 shell 命令,
>>> import subprocess
>>> subprocess.check_output("echo This_is_a_testing | grep -c test", shell=True)
'1\n'
但是,因为我不想使用“shell= True”选项,我尝试了以下python代码,
>>> import subprocess
>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
>>> p1.stdout.close()
>>> p2.communicate()
(None, None)
我想知道为什么输出是“None”,因为我参考了网页中的描述:http://docs.python.org/library/subprocess.html#subprocess.PIPE
我是否错过了代码中的一些要点?有什么建议/想法吗?提前致谢。
I have a case to want to execute the following shell command in Python and get the output,
echo This_is_a_testing | grep -c test
I could use this python code to execute the above shell command in python,
>>> import subprocess
>>> subprocess.check_output("echo This_is_a_testing | grep -c test", shell=True)
'1\n'
However, as I do not want to use the "shell=True" option, I tried the following python code,
>>> import subprocess
>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
>>> p1.stdout.close()
>>> p2.communicate()
(None, None)
I wonder why the output is "None" as I have referred to the descriptions in the webpage : http://docs.python.org/library/subprocess.html#subprocess.PIPE
Had I missed some points in my code ? Any suggestion / idea ? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请看这里:
在编写
p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
后,你会得到 1 作为输出,不要在您的问题中忽略此输出。如果这是您想要的,请将
stdout=subprocess.PIPE
作为参数传递给第二个Popen
:Please look here:
here you get 1 as output after you write
p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
, Do not ignore this output in the context of your question.If this is what you want, then pass
stdout=subprocess.PIPE
as argument to the secondPopen
:从手册中:
From the manual:
虽然接受的答案是正确/有效的,但另一种选择是使用 Popen.communicate() 方法将某些内容传递给进程的 stdin:
这解决了执行另一个命令只是为了重定向它的需要。输出,如果输出在 python 脚本本身中已知。
然而,通信有一个副作用,即它等待进程终止。如果需要/希望异步执行,使用两个进程可能是更好的选择。
While the accepted answer is correct/working, another option would be to use the
Popen.communicate()
method to pass something to a process' stdin:This resolves the need to execute another command just to redirect it's output, if the output is already known in the python script itself.
However
communicate
has the side-effect, that it waits for the process to terminate. If asynchronous execution is needed/desired using two processes might be the better option.答案与前面提到的类似,几乎没有格式。我想在 python 3 上使用管道获得与普通 shell 命令完全相同的输出。
Answer is similar to mentioned earlier, with little formatting. I wanted to get exactly same output as normal shell command with pipe on python 3.