<bdo id='Zm9Dd'></bdo><ul id='Zm9Dd'></ul>
    1. <small id='Zm9Dd'></small><noframes id='Zm9Dd'>

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

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

        忽略 subprocess.Popen 的输出

        时间:2023-07-21

        <small id='3wnYW'></small><noframes id='3wnYW'>

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

            <tfoot id='3wnYW'></tfoot>
              <bdo id='3wnYW'></bdo><ul id='3wnYW'></ul>
                <tbody id='3wnYW'></tbody>

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

                  本文介绍了忽略 subprocess.Popen 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在从我的 Python 脚本调用一个 java 程序,它输出了很多我不想要的无用信息.我已经尝试将 stdout=None 添加到 Popen 函数中:

                  I am calling a java program from my Python script, and it is outputting a lot of useless information I don't want. I have tried addind stdout=None to the Popen function:

                  subprocess.Popen(['java', '-jar', 'foo.jar'], stdout=None)
                  

                  但它也是一样的.有什么想法吗?

                  But it does the same. Any idea?

                  推荐答案

                  来自 3.3 文档:

                  stdin、stdout 和 stderr 分别指定执行程序的标准输入、标准输出和标准错误文件句柄.有效值为 PIPE、DEVNULL、现有文件描述符(正整数)、现有文件对象和无.

                  stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are PIPE, DEVNULL, an existing file descriptor (a positive integer), an existing file object, and None.

                  所以:

                  subprocess.check_call(['java', '-jar', 'foo.jar'], stdout=subprocess.DEVNULL)
                  

                  这仅存在于 3.3 及更高版本中.但是文档说:

                  This only exists in 3.3 and later. But the documentation says:

                  DEVNULL 表示将使用特殊文件 os.devnull.

                  DEVNULL indicates that the special file os.devnull will be used.

                  os.devnull 可以追溯到 2.4(在 subprocess 存在之前).因此,您可以手动执行相同的操作:

                  And os.devnull exists way back to 2.4 (before subprocess existed). So, you can do the same thing manually:

                  with open(os.devnull, 'w') as devnull:
                      subprocess.check_call(['java', '-jar', 'foo.jar'], stdout=devnull)
                  

                  请注意,如果您正在做一些不适合一行的更复杂的事情,您需要在 Popen 的整个生命周期内保持 devnull 处于打开状态对象,而不仅仅是它的构造.(也就是说,将整个内容放在 with 语句中.)

                  Note that if you're doing something more complicated that doesn't fit into a single line, you need to keep devnull open for the entire life of the Popen object, not just its construction. (That is, put the whole thing inside the with statement.)

                  重定向到 /dev/null (POSIX) 或 NUL: (Windows) 的优点是您不会创建不必要的管道,更重要的是,不能遇到子进程在写入该管道时阻塞的极端情况.

                  The advantage of redirecting to /dev/null (POSIX) or NUL: (Windows) is that you don't create an unnecessary pipe, and, more importantly, can't run into edge cases where the subprocess blocks on writing to that pipe.

                  缺点是,理论上,subprocess 可能在某些os.devnull 不具备的平台上工作.如果您只关心 POSIX 和 Windows、PyPy 和 Jython(大多数人)上的 CPython,那么这永远不会成为问题.对于其他情况,请在分发代码之前进行测试.

                  The disadvantage is that, in theory, subprocess may work on some platforms that os.devnull does not. If you only care about CPython on POSIX and Windows, PyPy, and Jython (which is most of you), this will never be a problem. For other cases, test before distributing your code.

                  这篇关于忽略 subprocess.Popen 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python subprocess.call() “没有这样的文件或目录" 下一篇:如何从 Python 迭代器提供子进程的标准输入?

                  相关文章

                    <tfoot id='kOeqE'></tfoot>

                        <bdo id='kOeqE'></bdo><ul id='kOeqE'></ul>

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

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

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