How to hide the console when using os.system() or subprocess.call() with Python?

Sometimes, we want to hide the console when using os.system() or subprocess.call() with Python.

In this article, we’ll look at how to hide the console when using os.system() or subprocess.call() with Python.

How to hide the console when using os.system() or subprocess.call() with Python?

To hide the console when using os.system() or subprocess.call() with Python we can use STARTUPINFO .

For instance, we write

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.call('taskkill /F /IM exename.exe', startupinfo=si)

to create the si variable with subprocess.STARTUPINFO().

Then we use

si.dwFlags |= subprocess.STARTF_USESHOWWINDOW

to hide the console window.

Then we run our command with subprocess.call.

We call it with the startupinfo argument set to si.

Conclusion

To hide the console when using os.system() or subprocess.call() with Python we can use STARTUPINFO .