subprocess.run不抑制所有控制台输出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了subprocess.run不抑制所有控制台输出相关的知识,希望对你有一定的参考价值。
[subprocess.run
与stdout=DEVNULL
和stderr=STDOUT
并不会抑制subinacl.exe实用程序的所有输出。
>>> # Do not suppress: OK
>>> subprocess.run('subinacl.exe /service "foo" display', shell=True)
foo - OpenService Error : 1060 The specified service does not exist as an installed service.
Elapsed Time: 00 00:00:00
Done: 1, Modified 0, Failed 1, Syntax errors 0
Last Done : foo
Last Failed: foo - OpenService Error : 1060 The specified service does not exist as an installed service.
CompletedProcess(args='subinacl.exe /service "foo" display', returncode=0)
>>> # Suppress: Some output is still printed
>>> subprocess.run('subinacl.exe /service "foo" display', shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
Elapsed Time: 00 00:00:00
Done: 1, Modified 0, Failed 1, Syntax errors 0
Last Done : foo
Last Failed: foo - OpenService Error : 1060 The specified service does not exist as an installed service.
CompletedProcess(args='subinacl.exe /service "foo" display', returncode=0)
>>>
我的猜测是subinacl.exe正在调用另一个进程,该进程打印未抑制的输出。 stdout=DEVNULL
和stderr=STDOUT
不会使整个过程链的输出静音吗?
答案
基于Sun的评论,我不得不使用
subprocess.run(
'subinacl.exe /service "foo" display',
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
creationflags=subprocess.CREATE_NO_WINDOW
)
以上是关于subprocess.run不抑制所有控制台输出的主要内容,如果未能解决你的问题,请参考以下文章
将 subprocess.run 输出重定向到 jupyter notebook 的
python 利用subprocess调用cmd命令程序,并正确输出控制台的输出中文