使用
我的猜测是subinacl.exe正在调用另一个进程来打印输出,而这个输出并没有被压制。使用
stdout=DEVNULL
和 stderr=STDOUT
的 subprocess.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 = DEVNULL
和stderr = STDOUT
会使整个处理链的输出被静音吗?
shell=True
,因为你没有运行一个 shell 命令。 - Eryk SunCreateConsoleScreenBuffer
创建新的屏幕缓冲区。如果您不想要任何可见输出,您有三个选项: - Eryk Suncreationflags=DETACHED_PROCESS
来运行它,无需控制台会话;在这种情况下,您应将所有未使用的标准句柄重定向到DEVNULL
。 (2)使用creationflags=CREATE_NO_WINDOW
来分配一个没有窗口的新的控制台会话(即GetConsoleWindow()
返回NULL
)。 (3)使用creationflags=CREATE_NEW_CONSOLE
和startupinfo=STARTUPINFO(dwFlags=STARTF_USESHOWWINDOW)
来分配一个带有隐藏窗口的新的控制台会话。 - Eryk Sunstdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW
。 - R01kcommunicate
动态读取管道的内容,就不应该使用管道。管道的容量有限(通常为4K)。当管道已满时,写入操作会被阻塞。这可能会阻塞程序的执行。如果您使用空设备,程序可以将内容写入标准输出或标准错误流而无需阻塞。 - Eryk Sun