pythonwindowssubprocesspython-3.7console-output

subprocess.run not suppressing all console output


subprocess.run with stdout=DEVNULL and stderr=STDOUT does not suppress all output from the subinacl.exe utility.

>>> # 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)
>>>

My guess is that subinacl.exe is calling another process that prints the output that is not being suppressed. Don't stdout=DEVNULL and stderr=STDOUT silence output from the whole process chain?


Solution

  • Based on Eryk Sun comment, I had to use

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