3

The title pretty much says it, although it does not need to be specific to a cmd, just closing an application in general. I have seen

os.system(taskkill blah blah)

this does not actually close the windows but rather just ends the cmd inside the window, I would like to actually close the window itself.

EDIT: Could someone please give me a specific line of code that would close a cmd window. The name of the cmd window when moused over is

C:\Windows\system32\cmd.exe
1
  • If there's more than one open, which one?
    – martineau
    Commented May 17, 2013 at 1:24

5 Answers 5

5

something like this?

import sys
sys.exit()

or easier ...

raise SystemExit

if that's not what your looking for tell me

also you can just save the file with a .pyw and that doesn't open the cmd at all

5
  • That just ends the python script. As mentioned in the python docs, Exit from Python. This is implemented by raising the SystemExit exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.
    – user1786283
    Commented May 17, 2013 at 0:48
  • I am not looking to close the python application itself, but rather an external application, for example a random cmd window.
    – Ulsting
    Commented May 17, 2013 at 1:28
  • ohhh ok my bad i was a bit confused
    – Serial
    Commented May 17, 2013 at 1:34
  • Do you perhaps know a way to close another application using python?
    – Ulsting
    Commented May 17, 2013 at 22:32
  • so you want to make a script that can close another one??
    – Serial
    Commented May 18, 2013 at 0:10
5

I used this to terminate and close the cmd window, hope it help os.system("taskkill /f /im cmd.exe")

4

For anyone who's stuck with Python 2.7 and unable to download pywin32 thanks to red tape in your organization. If you're on Windows XP and above you can use taskkill to kill the process by window title name like below for a dos command prompt that was started in elevated mode with title MyTitle

os.system('taskkill /fi "WindowTitle eq Administrator: MyTitle"')
3
  • 1
    I have used this approach but I had to include a second space character between the : and MyTitle. As per this post: stackoverflow.com/questions/10948235/…. Also /T might be useful if you are finding your application not closing. Commented Jul 2, 2014 at 6:48
  • 1
    The Administrator: part isn't doing anything special - this code simply looks for the window with that title (and elevated windows always have Administrator: in the title.) So this works just fine for non-elevated code, too. Commented Feb 4, 2015 at 15:41
  • This works for me in Windows 10 for non-elevated options: os.system('taskkill /fi "WindowTitle eq MyTitle"')
    – Alper
    Commented Jan 11, 2021 at 7:17
2

Here is an approach that uses the Python for Windows extensions (pywin32) to find the PIDs and taskill to end the process (based on this example). I went this way to give you access to some extra running information in case you didn't want to indiscriminately kill any cmd.exe:

import os
from win32com.client import GetObject

WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')

for p in WMI.ExecQuery('select * from Win32_Process where Name="cmd.exe"'):
    print "Killing PID:", p.Properties_('ProcessId').Value
    os.system("taskkill /pid "+str(p.Properties_('ProcessId').Value))

Now inside that for loop you could peek at some other information about each running process (or even look for child processes that depend on it (like running programs inside each cmd.exe). An example of how to read each process property might look like this:

from win32com.client import GetObject

WMI = GetObject('winmgmts:')
processes = WMI.InstancesOf('Win32_Process')

for p in WMI.ExecQuery('select * from Win32_Process where Name="cmd.exe"'):
    print "--running cmd.exe---"
    for prop in [prop.Name for prop in p.Properties_]:
        print prop,"=",p.Properties_(prop).Value
-1

the exit() command works however in 2.7 it still asks you if you are sure you want to quit. It states that the program is still running. ETC

Not the answer you're looking for? Browse other questions tagged or ask your own question.