• <bdo id='fN5iP'></bdo><ul id='fN5iP'></ul>

    <small id='fN5iP'></small><noframes id='fN5iP'>

    <i id='fN5iP'><tr id='fN5iP'><dt id='fN5iP'><q id='fN5iP'><span id='fN5iP'><b id='fN5iP'><form id='fN5iP'><ins id='fN5iP'></ins><ul id='fN5iP'></ul><sub id='fN5iP'></sub></form><legend id='fN5iP'></legend><bdo id='fN5iP'><pre id='fN5iP'><center id='fN5iP'></center></pre></bdo></b><th id='fN5iP'></th></span></q></dt></tr></i><div id='fN5iP'><tfoot id='fN5iP'></tfoot><dl id='fN5iP'><fieldset id='fN5iP'></fieldset></dl></div>

    1. <legend id='fN5iP'><style id='fN5iP'><dir id='fN5iP'><q id='fN5iP'></q></dir></style></legend>

      <tfoot id='fN5iP'></tfoot>
      1. Python - 如何使用管道执行shell命令,但没有'shell = True'?

        时间:2023-07-22
        • <small id='8pvFF'></small><noframes id='8pvFF'>

              <i id='8pvFF'><tr id='8pvFF'><dt id='8pvFF'><q id='8pvFF'><span id='8pvFF'><b id='8pvFF'><form id='8pvFF'><ins id='8pvFF'></ins><ul id='8pvFF'></ul><sub id='8pvFF'></sub></form><legend id='8pvFF'></legend><bdo id='8pvFF'><pre id='8pvFF'><center id='8pvFF'></center></pre></bdo></b><th id='8pvFF'></th></span></q></dt></tr></i><div id='8pvFF'><tfoot id='8pvFF'></tfoot><dl id='8pvFF'><fieldset id='8pvFF'></fieldset></dl></div>
              <tfoot id='8pvFF'></tfoot><legend id='8pvFF'><style id='8pvFF'><dir id='8pvFF'><q id='8pvFF'></q></dir></style></legend>

                <tbody id='8pvFF'></tbody>
                  <bdo id='8pvFF'></bdo><ul id='8pvFF'></ul>
                  本文介绍了Python - 如何使用管道执行shell命令,但没有'shell = True'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个案例想在 Python 中执行下面的 shell 命令并得到输出,

                  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
                  

                  我可以使用这个python代码在python中执行上面的shell命令,

                  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
                  '
                  

                  但是,由于我不想使用shell=True";选项,我尝试了下面的python代码,

                  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)
                  

                  我想知道为什么输出是无"?正如我在网页中提到的描述:http://docs.python.org/library/subprocess.html#subprocess.PIPE

                  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.

                  推荐答案

                  来自手册:

                  要在结果元组中获得除 None 以外的任何内容,您需要给出stdout=PIPE 和/或 stderr=PIPE

                  to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE

                  p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout, stdout=subprocess.PIPE)
                  

                  这篇关于Python - 如何使用管道执行shell命令,但没有'shell = True'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Popen 打开进程并获取 PID 下一篇:什么会导致“IOError: [Errno 9] Bad file descriptor"?在 os.syst

                  相关文章

                  <i id='Bgnbo'><tr id='Bgnbo'><dt id='Bgnbo'><q id='Bgnbo'><span id='Bgnbo'><b id='Bgnbo'><form id='Bgnbo'><ins id='Bgnbo'></ins><ul id='Bgnbo'></ul><sub id='Bgnbo'></sub></form><legend id='Bgnbo'></legend><bdo id='Bgnbo'><pre id='Bgnbo'><center id='Bgnbo'></center></pre></bdo></b><th id='Bgnbo'></th></span></q></dt></tr></i><div id='Bgnbo'><tfoot id='Bgnbo'></tfoot><dl id='Bgnbo'><fieldset id='Bgnbo'></fieldset></dl></div>
                • <tfoot id='Bgnbo'></tfoot>

                  <small id='Bgnbo'></small><noframes id='Bgnbo'>

                  • <bdo id='Bgnbo'></bdo><ul id='Bgnbo'></ul>

                    <legend id='Bgnbo'><style id='Bgnbo'><dir id='Bgnbo'><q id='Bgnbo'></q></dir></style></legend>