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 pyth开发者_运维百科on script.
I open the program throughos.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).
精彩评论