Python - 检测启动进程

Python - Detect a starting process

我想使用 Python 在 Windows 上检测启动进程。

例如:每当用户启动 notepad.exe 我想 运行 Python 中的一些功能.. 该脚本应等待这些进程启动。

我可以使用 Python 收集启动进程吗?

也许使用 'os' 或 'psutil' 模块?

希望你明白我的意思..你可以随时向我询问更多信息。

您可以通过轮询 Windows 系统命令来破解它,tasklist:

import time
import subprocess

while True:
    p = subprocess.run(['tasklist'], stdout = subprocess.PIPE)
    if b'notepad.exe' in p.stdout:
        print('Found!')
        break
    print('not yet...')
    time.sleep(1)

我不知道有什么方法可以中断一个进程的启动,以便 运行 在 另一个进程实际启动之前 某些东西。