32

Is there a way that python can close a windows application (example: Firefox) ?

I know how to start an app, but now I need to know how to close one.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
acrs
  • 563
  • 2
  • 8
  • 16
  • What kind of application do you mean? A python application? An external one? – Stedy Apr 11 '11 at 18:13
  • @Stedy Firefox, i'm using python to open a web page every 2 hours and i need to close that page after 5 minutes. – acrs Apr 11 '11 at 18:18
  • @user514584: why are you using an external program to open a webpage instead of using urllib? – Wooble Apr 11 '11 at 18:46
  • @Wooble I did not know about urllib and the page that i'm opening will need flash player. – acrs Apr 11 '11 at 18:55
  • @acrs To close an app by name, refer my code in the answer below https://stackoverflow.com/a/70849062/12862934 – Prashantzz Jan 26 '22 at 05:29

8 Answers8

31
# I have used subprocess comands for a while
# this program will try to close a firefox window every ten secounds

import subprocess
import time

# creating a forever loop
while 1 :
    subprocess.call("TASKKILL /F /IM firefox.exe", shell=True)
    time.sleep(10)
Community
  • 1
  • 1
gh057
  • 326
  • 3
  • 2
7

in windows you could use taskkill within subprocess.call:

subprocess.call(["taskkill","/F","/IM","firefox.exe"])

/F forces process termination. Omitting it only asks firefox to close, which can work if the app is responsive.

Cleaner/more portable solution with psutil (well, for Linux you have to drop the .exe part or use .startwith("firefox"):

import psutil,os
for pid in (process.pid for process in psutil.process_iter() if process.name()=="firefox.exe"):
    os.kill(pid)

that will kill all processes named firefox.exe

By the way os.kill(pid) is "overkill" (no pun intended). process has a kill() method, so:

for process in (process for process in psutil.process_iter() if process.name()=="firefox.exe"):
    process.kill()
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
7

If you're using Popen, you should be able to terminate the app using either send_signal(SIGTERM) or terminate().

See docs here.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
1

In order to kill a python tk window named MyappWindow under MS Windows:

from os import system
system('taskkill /F /FI "WINDOWTITLE eq MyappWindow" ')

stars maybe used as wildcard:

from os import system 
system('taskkill /F /FI "WINDOWTITLE eq MyappWind*" ')

Please, refer to "taskkill /?" for additional options.

Carlos ODL
  • 11
  • 1
1

You want probably use os.kill http://docs.python.org/library/os.html#os.kill

Elalfer
  • 5,312
  • 20
  • 25
1

An app(a running process) can be closed by it's name using it's PID(Process ID) and by using psutil module. Install it in cmd using the command:

pip install psutil

After installing, run the code given below in any .py file:

import psutil
def close_app(app_name):
    running_apps=psutil.process_iter(['pid','name']) #returns names of running processes
    found=False
    for app in running_apps:
        sys_app=app.info.get('name').split('.')[0].lower()

        if sys_app in app_name.split() or app_name in sys_app:
            pid=app.info.get('pid') #returns PID of the given app if found running
            
            try: #deleting the app if asked app is running.(It raises error for some windows apps)
                app_pid = psutil.Process(pid)
                app_pid.terminate()
                found=True
            except: pass
            
        else: pass
    if not found:
        print(app_name+" not found running")
    else:
        print(app_name+'('+sys_app+')'+' closed')

close_app('chrome')

After running the code above you may see the following output if google chrome was running:

>>> chrome(xyz) closed

Feel free to comment in case of any error

Prashantzz
  • 301
  • 2
  • 7
0

On OS X:

  1. Create a shell script and put:
killall Application

Replace Application with a running app of your choice.

In the same directory as this shell script, make a python file. In the python file, put these two lines of code:

from subprocess import Popen
Popen('sh shell.sh', shell=True)

Replace shell.sh with the name of your created shell script.

mehtaarn000
  • 176
  • 1
  • 3
  • 11
  • Is it possible to "gently" kill the app? so for example I want to kill excel but I still want it to ask me to save the sheet? – Joan Arau Apr 08 '21 at 14:11
  • @JoanArau I'm not really sure. Killing an app immediately terminates the process, so you shouldn't use that solution. – mehtaarn000 Apr 11 '21 at 16:50
0
from AppOpener import close
close("whatsapp") 
close("telegram, brave") 
close("name of ur application")

#pip install AppOpener

Nothing
  • 36
  • 5