python - 将子进程的输出拆分为列表/数组并在列表/数组中查找特定字符串。(Python)
问题描述
我正在寻找使用子进程来检查进程是否按名称运行以打开“powershell ps | findstr processname”并在它正在运行/未运行时执行某些操作。我不想使用 psutil 来完成此操作。
到目前为止,这是我想出的代码。
import subprocess
import sys
p = subprocess.Popen(['powershell.exe', 'ps | findstr chrome'],
stdout=subprocess.PIPE)
output = p.stdout.read()
s = output.split()
chrome = "chrome"
for _ in s:
if chrome in s:
print("chrome running")
else:
print("chrome not running")
这似乎不起作用。有谁知道如何完成这项任务?
解决方案
import subprocess
import sys
p = subprocess.Popen(['powershell.exe', 'ps | findstr chrome'],
stdout=subprocess.PIPE)
output = p.stdout.read()
s = output.split()
chrome = b"chrome"
if chrome in s:
print("chrome running")
else:
print("chrome not running")
推荐阅读
- javascript - 网站图像不会显示在 github 页面或 netlify 上
- java - 仅在编程 powershell 中不允许获取空标识名称(用于 <>)
- javascript - 文件中的 TypeScript 私有接口
- javascript - 为什么我的 DJS 角色声明消息可以工作几个小时,但突然停止?
- typeorm - 有没有办法动态加载实体?建立连接后加载实体
- javascript - 表单提交后 next.js 更新页面
- javascript - 在 JavaScript 的 Lodash 库中,如果开始编号大于结束编号,为什么它将开始编号与结束编号交换?
- node.js - 如何为两个单独的数据库使用相同的 Sequelize 模型?
- flutter - 键盘以意想不到的方式滚动屏幕
- android - 评估设置“android”时出现问题