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

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

    1. <tfoot id='wfphu'></tfoot>
        <bdo id='wfphu'></bdo><ul id='wfphu'></ul>
    2. <legend id='wfphu'><style id='wfphu'><dir id='wfphu'><q id='wfphu'></q></dir></style></legend>

      使用 StringIO 作为 Popen 的标准输入

      Use StringIO as stdin with Popen(使用 StringIO 作为 Popen 的标准输入)
      <i id='LsNtZ'><tr id='LsNtZ'><dt id='LsNtZ'><q id='LsNtZ'><span id='LsNtZ'><b id='LsNtZ'><form id='LsNtZ'><ins id='LsNtZ'></ins><ul id='LsNtZ'></ul><sub id='LsNtZ'></sub></form><legend id='LsNtZ'></legend><bdo id='LsNtZ'><pre id='LsNtZ'><center id='LsNtZ'></center></pre></bdo></b><th id='LsNtZ'></th></span></q></dt></tr></i><div id='LsNtZ'><tfoot id='LsNtZ'></tfoot><dl id='LsNtZ'><fieldset id='LsNtZ'></fieldset></dl></div>

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

      1. <tfoot id='LsNtZ'></tfoot>
          <bdo id='LsNtZ'></bdo><ul id='LsNtZ'></ul>

              <tbody id='LsNtZ'></tbody>

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

                本文介绍了使用 StringIO 作为 Popen 的标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下我想用 Python 编写的 shell 脚本(当然 grep . 实际上是一个更复杂的命令):

                I have the following shell script that I would like to write in Python (of course grep . is actually a much more complex command):

                #!/bin/bash
                
                (cat somefile 2>/dev/null || (echo 'somefile not found'; cat logfile)) 
                | grep .
                

                我试过这个(无论如何它都缺少与 cat logfile 等效的东西):

                I tried this (which lacks an equivalent to cat logfile anyway):

                #!/usr/bin/env python
                
                import StringIO
                import subprocess
                
                try:
                    myfile = open('somefile')
                except:
                    myfile = StringIO.StringIO('somefile not found')
                
                subprocess.call(['grep', '.'], stdin = myfile)
                

                但我得到错误 AttributeError: StringIO instance has no attribute 'fileno'.

                我知道我应该使用 subprocess.communicate() 而不是 StringIO 将字符串发送到 grep 进程,但我不知道如何混合使用这两个字符串和文件.

                I know I should use subprocess.communicate() instead of StringIO to send strings to the grep process, but I don't know how to mix both strings and files.

                推荐答案

                p = subprocess.Popen(['grep', '...'], stdin=subprocess.PIPE, 
                                                      stdout=subprocess.PIPE)
                output, output_err = p.communicate(myfile.read())
                

                这篇关于使用 StringIO 作为 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 画布滚动条?)

                  <tbody id='s59Z4'></tbody>

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

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

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

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