Python 忽略来自 subprocess.Popen 的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14735001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:16:58  来源:igfitidea点击:

Ignoring output from subprocess.Popen

pythonsubprocess

提问by Roman Rdgz

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=Noneto the Popen function:

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

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

But it does the same. Any idea?

但它的作用相同。任何的想法?

采纳答案by abarnert

From the 3.3 documentation:

3.3 文档

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.

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

So:

所以:

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

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

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

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

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

And os.devnullexists way back to 2.4 (before subprocessexisted). So, you can do the same thing manually:

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

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

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

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

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.

重定向到/dev/null(POSIX) 或NUL:(Windows)的优点是您不会创建不必要的管道,更重要的是,不会遇到子进程阻止写入该管道的边缘情况。

The disadvantage is that, in theory, subprocessmay work on some platforms that os.devnulldoes 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可能在某些os.devnull不工作的平台上工作。如果您只关心 POSIX 和 Windows 上的 CPython、PyPy 和 Jython(你们中的大多数人),这永远不会成为问题。对于其他情况,请在分发代码之前进行测试。

回答by Martijn Pieters

From the documentation:

文档

With the default settings of None, no redirection will occur.

使用默认设置 时None,不会发生重定向。

You need to set stdoutto subprocess.PIPE, then call .communicate()and simply ignore the captured output.

您需要设置stdoutsubprocess.PIPE,然后调用.communicate()并忽略捕获的输出。

p = subprocess.Popen(['java', '-jar', 'foo.jar'], stdout=subprocess.PIPE)
p.communicate()

although I suspect that using subprocess.call()more than suffices for your needs:

虽然我怀疑使用subprocess.call()超过满足您的需求:

subprocess.call(['java', '-jar', 'foo.jar'], stdout=subprocess.PIPE)