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

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

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

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

      <tfoot id='ysKl1'></tfoot>

      使用子进程运行多个 bash 命令

      running multiple bash commands with subprocess(使用子进程运行多个 bash 命令)
      <tfoot id='QQCWZ'></tfoot>

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

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

              <tbody id='QQCWZ'></tbody>

              • <legend id='QQCWZ'><style id='QQCWZ'><dir id='QQCWZ'><q id='QQCWZ'></q></dir></style></legend>
                本文介绍了使用子进程运行多个 bash 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如果我运行 echo a;echo b in bash 结果将是两个命令都运行.但是,如果我使用 subprocess 则运行第一个命令,打印出整个行的其余部分.下面的代码回显 a;echo b 而不是 a b,如何让它同时运行这两个命令?

                If I run echo a; echo b in bash the result will be that both commands are run. However if I use subprocess then the first command is run, printing out the whole of the rest of the line. The code below echos a; echo b instead of a b, how do I get it to run both commands?

                import subprocess, shlex
                def subprocess_cmd(command):
                    process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
                    proc_stdout = process.communicate()[0].strip() 
                    print proc_stdout
                
                subprocess_cmd("echo a; echo b")
                

                推荐答案

                你必须在子进程中使用 shell=True 并且没有 shlex.split:

                You have to use shell=True in subprocess and no shlex.split:

                import subprocess
                
                command = "echo a; echo b"
                
                ret = subprocess.run(command, capture_output=True, shell=True)
                
                # before Python 3.7:
                # ret = subprocess.run(command, stdout=subprocess.PIPE, shell=True)
                
                print(ret.stdout.decode())
                

                返回:

                a
                b
                

                这篇关于使用子进程运行多个 bash 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 画布滚动条?)

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

                • <legend id='HYpSG'><style id='HYpSG'><dir id='HYpSG'><q id='HYpSG'></q></dir></style></legend>

                    <tbody id='HYpSG'></tbody>
                  • <bdo id='HYpSG'></bdo><ul id='HYpSG'></ul>
                  • <tfoot id='HYpSG'></tfoot>

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