OCW
OCW

Reputation: 93

Python - Cannot Kill Process

I'm using Python 3.5 and Windows 7. I'm trying to open an exe file and then terminate it. I've succeeded in opening it, but I can't close it. Here's an abbreviated version of my code:

from subprocess import Popen

open = Popen(["filename"], shell = True, cwd = "path\to\file")
open.terminate()

I've also tried open.kill(), os.kill(open.pid, 0) and os.system("TASKKILL /IM /F filename.exe"). I also found a post suggesting to not use shell = True, but I haven't been able to get Popen to work without it. None of these options throw errors, they just don't close the file I opened.

I've found similar errors online, but there doesn't seem to be a concrete solution to this. The potential solutions I read (the most common listed above) have not worked for me. Is there a solution to this?

Upvotes: 3

Views: 1022

Answers (1)

OCW
OCW

Reputation: 93

It turns out shell = False is necessary for this to work. Here is my successful code:

open = Popen(["path\to\file\filename"])
open.terminate()

Upvotes: 1

Related Questions