<bdo id='2OpOT'></bdo><ul id='2OpOT'></ul>

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

  • <legend id='2OpOT'><style id='2OpOT'><dir id='2OpOT'><q id='2OpOT'></q></dir></style></legend>

        <tfoot id='2OpOT'></tfoot>

        <small id='2OpOT'></small><noframes id='2OpOT'>

      1. Python subprocess 模块,我如何为管道命令系列中的第一个提供输入?

        时间:2023-07-21

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

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

              <tbody id='lWOHP'></tbody>

              • <bdo id='lWOHP'></bdo><ul id='lWOHP'></ul>
                  <legend id='lWOHP'><style id='lWOHP'><dir id='lWOHP'><q id='lWOHP'></q></dir></style></legend>

                • <tfoot id='lWOHP'></tfoot>
                  本文介绍了Python subprocess 模块,我如何为管道命令系列中的第一个提供输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Python 的 subprocess 模块.我需要的是将输入发送到第一个进程,其输出成为第二个进程的输入.情况与此处文档中给出的示例基本相同:http://docs.python.org/library/subprocess.html#replacing-shell管道除了我需要提供输入第一个命令.这是复制的示例:

                  I am trying to use Python's subprocess module. What I require is to send input to the first process whose output becomes the input of the second process. The situation is basically almost the same as the example given in the documentation here: http://docs.python.org/library/subprocess.html#replacing-shell-pipeline except that I need to provide input the first command. Here is that example copied:

                  p1 = Popen(["dmesg"], stdout=PIPE)
                  p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
                  p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
                  output = p2.communicate()[0]
                  

                  如果我们将第一行改为:

                  If we change the first line to:

                  p1 = Popen(["cat"], stdout=PIPE, stdin=PIPE)
                  

                  如何向进程提供输入字符串?如果我尝试通过将最后一行更改为:

                  How do I provide the input string to the process? If I attempt it by changing the final line to:

                  output = p2.communicate(input=inputstring)[0]
                  

                  这不起作用.

                  我确实有一个工作版本,它只是将第一个命令的输出存储在一个字符串中,然后将其传递给第二个命令.这并不可怕,因为基本上没有可以利用的并发性(在我的实际用例中,第一个命令将很快退出并在最后产生所有输出).这是完整的工作版本:

                  I do have a working version, which just stores the output of the first command in a string and then passes that to the second command. This isn't terrible as there is essentially no concurrency that can be exploited (in my actual use case the first command will exit rather quickly and produce all of its output at the end). Here is the working version in full:

                  import subprocess
                  
                  simple = """Writing some text
                  with some lines in which the
                  word line occurs but others
                  where it does
                  not
                  """
                  
                  def run ():
                    catcommand = [ "cat" ]
                    catprocess = subprocess.Popen(catcommand,
                                                  stdin=subprocess.PIPE,
                                                  stdout=subprocess.PIPE,
                                                  stderr=subprocess.PIPE)
                    (catout, caterr) = catprocess.communicate(input=simple)
                    grepcommand = [ "grep", "line" ]
                    grepprocess = subprocess.Popen(grepcommand,
                                                  stdin=subprocess.PIPE,
                                                  stdout=subprocess.PIPE,
                                                  stderr=subprocess.PIPE)
                    (grepout, greperr) = grepprocess.communicate(input=catout)
                    print "--- output ----"
                    print grepout 
                    print "--- error ----"
                    print greperr 
                  
                  if __name__ == "__main__":
                    run()
                  

                  我希望我已经足够清楚了,感谢您的帮助.

                  I hope I've been clear enough, thanks for any help.

                  推荐答案

                  如果你这样做了

                  from subprocess import Popen, PIPE
                  p1 = Popen(["cat"], stdout=PIPE, stdin=PIPE)
                  

                  您应该执行 p1.communicate("Your Input to the p1") ,这将流经 PIPE.标准输入是进程的输入,您应该只与它通信.

                  You should do p1.communicate("Your Input to the p1") and that will flow through the PIPE. The stdin is the process's input and you should communicate to that only.

                  给出的程序完全没问题,好像没有问题.

                  The program which have given is absolutely fine, there seems no problem with that.

                  这篇关于Python subprocess 模块,我如何为管道命令系列中的第一个提供输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:subprocess.Popen 带有 unicode 路径 下一篇:出现错误 - AttributeError: 'module' object has no attrib

                  相关文章

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

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

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