dawnoflife
dawnoflife

Reputation: 1652

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 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!

Upvotes: 1

Views: 7415

Answers (3)

Greg Hewgill
Greg Hewgill

Reputation: 992887

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).

Upvotes: 0

Dan
Dan

Reputation: 18694

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

Upvotes: 3

Eryk Sun
Eryk Sun

Reputation: 34260

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()

Upvotes: 0

Related Questions