我正在编写一个写CPU和RAM使用情况的脚本,我想使用1%CPU或RAM添加每个进程的列表
import datetime ,psutil ,time ,subprocess
mem_usage = (psutil.virtual_memory())
cpu_usage = psutil.cpu_percent(interval=1)
cmd = "WMIC PROCESS get Caption,ProcessId"
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
d_h = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S")
f = open('dane.txt', 'a')
f.write("DATE: "+d_h+'\n')
f.write("MEM USAGE: "+(str(mem_usage)+'\n'))
f.write("CPU USAGE: "+(str(cpu_usage)+"%"'\n'))
f.write("CPU PROCESS LIST: "+""+'\n')
for proc in psutil.process_iter():
f.write(str(proc.name)+'\n')
f.write(" "+'\n')
f.close()
我希望它看起来像这样
file.txt
DATE & TIME
CPU_USAGE: 45%
MEM_USAGE: 88%
PROCESS_LIST:
list all process usage 1% RAM or CPU
预先感谢
答案 0 :(得分:1)
for proc in psutil.process_iter():
f.write(str(proc.name())+'\n')