subprocess.run未抑制所有控制台输出

2024-09-30 06:15:34 发布

您现在位置:Python中文网/ 问答频道 /正文

带有stdout=DEVNULLstderr=STDOUTsubprocess.run不会抑制来自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=DEVNULLstderr=STDOUT使整个流程链的输出静音吗


Tags: runfooservicedisplaystderrstdoutnotexe
1条回答
网友
1楼 · 发布于 2024-09-30 06:15:34

根据Erik Sun的评论,我不得不使用

subprocess.run(
    'subinacl.exe /service "foo" display',
    stdout=subprocess.DEVNULL,
    stderr=subprocess.STDOUT, 
    creationflags=subprocess.CREATE_NO_WINDOW
)

相关问题 更多 >

    热门问题