سؤال

I am trying to launch an exe from my python script using

subprocess

but I want to close/kill it from python script too. Is there any way I can accomplish stopping an exe from python?

PS: My executable stops when I do Alt+F4. I basically want to emulate Alt+F4 programmatically in python.

هل كانت مفيدة؟

المحلول

Sure, you want to call Popen.terminate() or Popen.kill() on the subprocess. e.g.:

p = subprocess.Popen(['path/to/long/runnning/process', 'arg1', 'arg2'])
...
p.terminate()  # kill the process.

Popen.terminate is a little more gentle -- On POSIX OSs, it'll give the program a chance to handle the signal and do some cleanup vs. kill which will try to destroy the process immediately.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top