How do I hide the console when I use os.system() or

Posted on

Solving problem is about exposing yourself to as many situations as possible like How do I hide the console when I use os.system() or and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How do I hide the console when I use os.system() or, which can be followed any time. Take easy to follow this discuss.

How do I hide the console when I use os.system() or

I wrote some statements like below:

os.system(cmd) #do something'taskkill /F /IM exename.exe')

both will pop up a console.

How can I stop it from popping up the console?

Asked By: Synapse


Answer #1:

The process STARTUPINFO can hide the console window:

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#si.wShowWindow = subprocess.SW_HIDE # default'taskkill /F /IM exename.exe', startupinfo=si)

Or set the creation flags to disable creating the window:

CREATE_NO_WINDOW = 0x08000000'taskkill /F /IM exename.exe', creationflags=CREATE_NO_WINDOW)

The above is still a console process with valid handles for console I/O (verified by calling GetFileType on the handles returned by GetStdHandle). It just has no window and doesn’t inherit the parent’s console, if any.

You can go a step farther by forcing the child to have no console at all:

DETACHED_PROCESS = 0x00000008'taskkill /F /IM exename.exe', creationflags=DETACHED_PROCESS)

In this case the child’s standard handles (i.e. GetStdHandle) are 0, but you can set them to an open disk file or pipe such as subprocess.DEVNULL (3.3) or subprocess.PIPE.

Answered By: Eryk Sun

Answer #2:

Add the shell=True argument to the subprocess calls.'taskkill /F /IM exename.exe', shell=True)

Or, if you don’t need to wait for it, use subprocess.Popen rather than

subprocess.Popen('taskkill /F /IM exename.exe', shell=True)
Answered By: Chris Morgan

Answer #3:

Just add:'powershell.exe taskkill /F /IM exename.exe', shell=True)

Answered By: Xenoy

Answer #4:

Try to change the extension from .py to .pyw

Its basically just a Python User Interface file. So it opens up a new Window without the command line. chech this link (

Answered By: neferjina

Answer #5:

Try subprocess.Popen(["function","option1","option2"],shell=False).

Answered By: ambagesia
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published. Required fields are marked *