1

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!

dawnoflife
  • 1,572
  • 10
  • 42
  • 62
  • Seems like the top answer to this question would do what you're looking for: http://stackoverflow.com/questions/5780425/run-external-program-in-a-loop-with-max-time-limit – Dan H Jul 11 '11 at 04:34
  • What if I don't want to go through the subprocess method? – dawnoflife Jul 11 '11 at 04:38

3 Answers3

3

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

Dan H
  • 17,654
  • 1
  • 27
  • 22
0

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()
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
0

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

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285