if pinfo['name'] == 'python.exe' and 'OI_YYH.py' in pinfo['cmdline'] and pinfo['status'] == psutil.STATUS_RUNNING: KeyError: 'cmdline'
时间: 2023-10-14 16:06:06 浏览: 147
抱歉,代码更新不够及时,这是因为在Windows系统上,psutil的process对象没有"cmdline"属性,所以需要使用"create_time"和"cmdline()"方法来判断Python程序是否正在运行。以下是更新后的代码:
```python
import psutil
for proc in psutil.process_iter(['pid', 'name', 'create_time', 'cmdline']):
try:
pinfo = proc.info
except psutil.NoSuchProcess:
pass
else:
if pinfo['name'] == 'python.exe' and 'OI_YYH.py' in ' '.join(pinfo['cmdline']) and pinfo['create_time'] > 0:
print('Target program is running')
break
else:
print('Target program is not running')
```
这里的代码会遍历每个进程,如果找到了名为"python.exe"且命令行参数包含"OI_YYH.py"且创建时间大于0的进程,则输出"Target program is running",否则输出"Target program is not running"。
注意:需要替换代码中的"OI_YYH.py"为目标程序的文件名。
阅读全文