如何在 Windows 上杀死 Python 中的 script.py?

How to kill script.py in Python on Windows?

在 Python 中是否可以仅使用脚本的文件名关闭脚本?我有两个脚本不能同时工作,所以在启动时我希望其中一个脚本结束另一个脚本。这可能吗?如果是这样,请告诉我怎么做。

非常感谢任何帮助。谢谢,

Mitra0000

在这个answer中,它说你可以。您将需要一个 "PID (Process Identifier)" 并知道您使用的是 Unix 还是 Python。为什么?因为有两种不同的方式:

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)

回答中也提到:

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.