简体   繁体   中英

In Windows, Killing an application through Python

I am trying to kill external running applications like Windows Paint , a .mp3 and similar programs through a python script.

I open the program through os.startfile . Any ideas how I can close the programs efficiently? I am using a Windows 7 machine. I'd appreciate the help a lot! Thanks!

As of Python 2.7 os.kill works on Windows. You could find the PID using this recipe .

Popen will let you kill an opened process, but that won't prompt to save files, etc.

p = subprocess.Popen(['notepad', 'tmp.txt'])
#later
p.kill()

When you ask Windows to launch something with os.startfile() , you don't get a handle to the created process. This is because the underlying ShellExecute() function does not return a handle to the created process. I believe this is because not all actions actually result in a process being created at all (for example with in-process COM servers or other esoteria).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM