<tfoot id='FoQgF'></tfoot>
        <bdo id='FoQgF'></bdo><ul id='FoQgF'></ul>

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

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

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

        用管道链接几个 Popen 命令

        link several Popen commands with pipes(用管道链接几个 Popen 命令)
        <i id='yznfU'><tr id='yznfU'><dt id='yznfU'><q id='yznfU'><span id='yznfU'><b id='yznfU'><form id='yznfU'><ins id='yznfU'></ins><ul id='yznfU'></ul><sub id='yznfU'></sub></form><legend id='yznfU'></legend><bdo id='yznfU'><pre id='yznfU'><center id='yznfU'></center></pre></bdo></b><th id='yznfU'></th></span></q></dt></tr></i><div id='yznfU'><tfoot id='yznfU'></tfoot><dl id='yznfU'><fieldset id='yznfU'></fieldset></dl></div>

            <tbody id='yznfU'></tbody>

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

        • <tfoot id='yznfU'></tfoot>
              <legend id='yznfU'><style id='yznfU'><dir id='yznfU'><q id='yznfU'></q></dir></style></legend>
                <bdo id='yznfU'></bdo><ul id='yznfU'></ul>
                  本文介绍了用管道链接几个 Popen 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道如何使用 cmd = subprocess.Popen 然后 subprocess.communicate 来运行命令.大多数时候,我使用一个用 shlex.split 标记的字符串作为 Popen 的 'argv' 参数.以ls -l"为例:

                  导入子流程进口shlex打印 subprocess.Popen(shlex.split(r'ls -l'),stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE).communicate()[0]

                  但是,管道似乎不起作用...例如,以下示例返回注意:

                  导入子流程进口shlexprint subprocess.Popen(shlex.split(r'ls -l | sed "s/a/b/g"'), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0]

                  你能告诉我我做错了什么吗?

                  谢谢

                  解决方案

                  我想你想在这里实例化两个单独的 Popen 对象,一个用于ls",另一个用于sed".您需要将第一个 Popen 对象的 stdout 属性作为 stdin 参数传递给第二个 Popen 对象.

                  例子:

                  p1 = subprocess.Popen('ls ...', stdout=subprocess.PIPE)p2 = subprocess.Popen('sed ...', stdin=p1.stdout, stdout=subprocess.PIPE)打印 p2.communicate()

                  如果你有更多命令,你可以继续这样链接:

                  p3 = subprocess.Popen('prog', stdin=p2.stdout, ...)

                  有关如何使用子流程的更多信息,请参阅子流程文档.p>

                  I know how to run a command using cmd = subprocess.Popen and then subprocess.communicate. Most of the time I use a string tokenized with shlex.split as 'argv' argument for Popen. Example with "ls -l":

                  import subprocess
                  import shlex
                  print subprocess.Popen(shlex.split(r'ls -l'), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0]
                  

                  However, pipes seem not to work... For instance, the following example returns noting:

                  import subprocess
                  import shlex
                  print subprocess.Popen(shlex.split(r'ls -l | sed "s/a/b/g"'), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0]
                  

                  Can you tell me what I am doing wrong please?

                  Thx

                  解决方案

                  I think you want to instantiate two separate Popen objects here, one for 'ls' and the other for 'sed'. You'll want to pass the first Popen object's stdout attribute as the stdin argument to the 2nd Popen object.

                  Example:

                  p1 = subprocess.Popen('ls ...', stdout=subprocess.PIPE)
                  p2 = subprocess.Popen('sed ...', stdin=p1.stdout, stdout=subprocess.PIPE)
                  print p2.communicate()
                  

                  You can keep chaining this way if you have more commands:

                  p3 = subprocess.Popen('prog', stdin=p2.stdout, ...)
                  

                  See the subprocess documentation for more info on how to work with subprocesses.

                  这篇关于用管道链接几个 Popen 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

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

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

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