Python 如何只调用命令而不获取其输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4996852/
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 18:33:44  来源:igfitidea点击:

How to just call a command and not get its output

pythonsubprocess

提问by user225312

In Python, what is the shortest and the standard way of calling a command through subprocessbut not bothering with its output.

在 Python 中,调用命令subprocess但不打扰其输出的最短和标准方法是什么。

I tried subprocess.callhowever it seems to return the output. I am not bothered with that, I just need to run the program silently without the output cluttering up the screen.

我试过了,subprocess.call但它似乎返回了输出。我对此并不介意,我只需要安静地运行程序,而不会让输出在屏幕上杂乱无章。

If it helps, I am callling pdflatexand my intention is just to call it.

如果有帮助,我会打电话pdflatex,我的目的只是打电话。

采纳答案by Santa

p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
# do something with out, err, or don't bother altogether.

Basically, this "pipes" whatever cmdoutputs to stdout and stderr to in-memory buffers prepared by subprocess. What you do with the content of those buffers are up to you. You can examine them, or don't bother with them altogether. But the side effect of piping to these buffers is that they won't be printed to the terminal's.

基本上,这将cmd输出到 stdout 和 stderr 的任何输出“管道”到由subprocess. 您如何处理这些缓冲区的内容取决于您。您可以检查它们,或者完全不理会它们。但是管道到这些缓冲区的副作用是它们不会被打印到终端。

edit: This also works with the convenience method, call. For a demonstration:

编辑:这也适用于方便的方法,call。演示:

>>> subprocess.call('ping 127.0.0.1')

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
0
>>> subprocess.call('ping 127.0.0.1', stdout=subprocess.PIPE)
0

edit-2: A note of caution for subprocess.call:

编辑 2:注意事项subprocess.call

Note: Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.

注意:不要将 stdout=PIPE 或 stderr=PIPE 与此函数一起使用。由于当前进程没有读取管道,如果子进程生成足够的输出到管道以填满操作系统管道缓冲区,它可能会阻塞。

回答by Tyler Eaves

just call it as you are and tack >/dev/null on the end of the comamnd. That will redirect any textual output.

只需按原样调用它并在命令末尾添加 >/dev/null 即可。这将重定向任何文本输出。

回答by Sven Marnach

In case your process produces significant amounts of output that you don't want to buffer in memory, you should redirect the output to the electronic trash can:

如果您的流程产生了大量您不想缓冲在内存中的输出,您应该将输出重定向到电子垃圾桶:

with open(os.devnull, "w") as f:
    subprocess.call(["pdflatex", filename], stdout=f)

The variable os.devnullis the name of the null device of your operating system (/dev/nullon most OSes, nulon the other one).

该变量os.devnull是操作系统的空设备的名称(/dev/null在大多数操作系统上,nul在另一个操作系统上)。

回答by Subhankar Ghosal

Use the /dev/null if you are using Unix. If you run any command in Shell and don't want to show its output on terminal.

如果您使用的是 Unix,请使用 /dev/null。如果您在 Shell 中运行任何命令并且不想在终端上显示其输出。

For example :- ls > /dev/null will not produce any output on terminal.

例如:- ls > /dev/null 不会在终端上产生任何输出。

So just use os,subprocess to execute some thing on shell and just put its o/p into /dev/null.

因此,只需使用 os,subprocess 在 shell 上执行某些操作,然后将其 o/p 放入 /dev/null。