–
import subprocess
import re
def get_cpu_usage():
# 执行 WMIC 命令并获取 CPU 占用率
cpu_usage_info = subprocess.check_output("wmic cpu get loadpercentage", shell=True).decode()
# 使用正则表达式提取 CPU 占用率的数值
cpu_usage_match = re.search(r'([0-9]+)', cpu_usage_info)
if cpu_usage_match:
cpu_usage = int(cpu_usage_match.group(1))
return str(cpu_usage)+'%'
else:
print("Failed to extract CPU usage information.提取CPU使用率信息失败.")
return None
–
发表回复