How to kill script.py in Python on Windows?

685 views Asked by At

Is it possible in Python to close a script using just the filename of the script? I have two scripts which don't work at the same time so on startup I want one of the scripts to end the other script. Is this possible? If so, please let me know how.

Any help greatly appreciated. Thanks,

Mitra0000

1

There are 1 answers

1
Anthony Pham On BEST ANSWER

In this answer, it says that you can. You would need a "PID (Process Identifier)" and know whether you are using Unix or Python. Why? Because there are two different ways:

Unix:

import os, signal
os.kill(5383, signal.SIGKILL)

Windows:

import subprocess as s
def killProcess(pid):
    s.Popen('taskkill /F /PID {0}'.format(pid), shell=True)

The answer also mentioned:

You can send the PID to the other programm or you could search in the process-list to find the name of the other script and kill it with the above script.