<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 的标准输入

      时间:2023-07-21
      <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 的标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:python:非阻塞子进程,检查标准输出 下一篇:从 python 脚本运行 C# 应用程序

                相关文章

                <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>