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

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

  • <tfoot id='ZzQp3'></tfoot>

      1. <i id='ZzQp3'><tr id='ZzQp3'><dt id='ZzQp3'><q id='ZzQp3'><span id='ZzQp3'><b id='ZzQp3'><form id='ZzQp3'><ins id='ZzQp3'></ins><ul id='ZzQp3'></ul><sub id='ZzQp3'></sub></form><legend id='ZzQp3'></legend><bdo id='ZzQp3'><pre id='ZzQp3'><center id='ZzQp3'></center></pre></bdo></b><th id='ZzQp3'></th></span></q></dt></tr></i><div id='ZzQp3'><tfoot id='ZzQp3'></tfoot><dl id='ZzQp3'><fieldset id='ZzQp3'></fieldset></dl></div>
      2. 如何将字符串传递给 subprocess.Popen(使用 stdin 参数)?

        时间:2023-07-22
      3. <tfoot id='KSYa2'></tfoot>
          <bdo id='KSYa2'></bdo><ul id='KSYa2'></ul>

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

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

                <tbody id='KSYa2'></tbody>

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

                  本文介绍了如何将字符串传递给 subprocess.Popen(使用 stdin 参数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我执行以下操作:

                  导入子流程从 cStringIO 导入 StringIOsubprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one
                  two
                  three
                  four
                  five
                  six
                  ')).communicate()[0]

                  我明白了:

                  Traceback(最近一次调用最后一次):文件<stdin>",第 1 行,在 ?__init__ 中的文件/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py",第 533 行(p2cread,p2cwrite,_get_handles 中的文件/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py",第 830 行p2cread = stdin.fileno()AttributeError:cStringIO.StringI"对象没有属性fileno"

                  显然,cStringIO.StringIO 对象与文件鸭的距离不足以适应 subprocess.Popen.我该如何解决这个问题?

                  解决方案

                  Popen.communicate() 文档:

                  <块引用>

                  注意,如果你想发送数据到进程的标准输入,你需要创建 Popen 对象标准输入=管道.同样,得到任何东西除了结果元组中的 None 之外,你需要给 stdout=PIPE 和/或stderr=PIPE 也是.

                  替换 os.popen*

                   pipe = os.popen(cmd, 'w', bufsize)# ==>pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin

                  <块引用>

                  警告使用communicate()而不是stdin.write()、stdout.read() 或stderr.read() 以避免死锁到任何其他操作系统管道缓冲区填满并阻止孩子过程.

                  所以你的例子可以写成如下:

                  from subprocess import Popen, PIPE, STDOUTp = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)grep_stdout = p.communicate(输入=b'一个
                  两个
                  三个
                  四个
                  五个
                  six
                  ')[0]打印(grep_stdout.decode())#->四#->五#->


                  在 Python 3.5+(encoding 为 3.6+)上,您可以使用 subprocess.run,将输入作为字符串传递给外部命令并获取其退出状态,并在一次调用中将其输出作为字符串返回:

                  #!/usr/bin/env python3从子流程导入运行,管道p =运行(['grep','f'],标准输出=管道,输入='一
                  二
                  三
                  四
                  五
                  six
                  ',编码='ascii')打印(p.returncode)#->0打印(p.stdout)#->四#->五#->

                  If I do the following:

                  import subprocess
                  from cStringIO import StringIO
                  subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one
                  two
                  three
                  four
                  five
                  six
                  ')).communicate()[0]
                  

                  I get:

                  Traceback (most recent call last):
                    File "<stdin>", line 1, in ?
                    File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 533, in __init__
                      (p2cread, p2cwrite,
                    File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 830, in _get_handles
                      p2cread = stdin.fileno()
                  AttributeError: 'cStringIO.StringI' object has no attribute 'fileno'
                  

                  Apparently a cStringIO.StringIO object doesn't quack close enough to a file duck to suit subprocess.Popen. How do I work around this?

                  解决方案

                  Popen.communicate() documentation:

                  Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

                  Replacing os.popen*

                      pipe = os.popen(cmd, 'w', bufsize)
                      # ==>
                      pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin
                  

                  Warning Use communicate() rather than stdin.write(), stdout.read() or stderr.read() to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

                  So your example could be written as follows:

                  from subprocess import Popen, PIPE, STDOUT
                  
                  p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)    
                  grep_stdout = p.communicate(input=b'one
                  two
                  three
                  four
                  five
                  six
                  ')[0]
                  print(grep_stdout.decode())
                  # -> four
                  # -> five
                  # ->
                  


                  On Python 3.5+ (3.6+ for encoding), you could use subprocess.run, to pass input as a string to an external command and get its exit status, and its output as a string back in one call:

                  #!/usr/bin/env python3
                  from subprocess import run, PIPE
                  
                  p = run(['grep', 'f'], stdout=PIPE,
                          input='one
                  two
                  three
                  four
                  five
                  six
                  ', encoding='ascii')
                  print(p.returncode)
                  # -> 0
                  print(p.stdout)
                  # -> four
                  # -> five
                  # -> 
                  

                  这篇关于如何将字符串传递给 subprocess.Popen(使用 stdin 参数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python - subprocess.Popen().pid 返回父脚本的pid 下一篇:检索 subprocess.call() 的输出

                  相关文章

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

                  <tfoot id='WsdMf'></tfoot>

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

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