
正文
Python psutil模块使用
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
import psutil # 获取内存信息
mem = psutil.virtual_memory()
total = mem.total / 1024 / 1024 / 1024
used = mem.used / 1024 / 1024 / 1024
print('内存总量:' + str(round(total, 2)) + 'G,已使用:' + str(round(used, 2)) + 'G') print(psutil.cpu_times())
# 获取CPU的逻辑个数,默认logical为True
logical_count = psutil.cpu_count()
physical_count = psutil.cpu_count(logical=False)
print('逻辑处理器个数为:' + str(logical_count) + "\n物理处理器个数为:" + str(physical_count)) # 磁盘信息
print(psutil.disk_partitions()) # 完整磁盘信息
disk_c = psutil.disk_usage('c:\\')
disk_d = psutil.disk_usage('d:\\')
unit_gb = 1024 * 1024 * 1024
print(
'C盘总容量:' + str(round(disk_c.total / unit_gb, 2)) + 'G,已使用:' + str(round(disk_c.used / unit_gb, 2)) + 'G,未使用:' + str(
round(disk_c.free / unit_gb, 2)) + 'G,使用百分比:' + str(disk_c.percent) + '%') # 获取分区(参数)的使用情况
print(
'D盘总容量:' + str(round(disk_d.total / unit_gb, 2)) + 'G,已使用:' + str(round(disk_d.used / unit_gb, 2)) + 'G,未使用:' + str(
round(disk_d.free / unit_gb, 2)) + 'G使用百分比:' + str(disk_d.percent) + '%') # 获取分区(参数)的使用情况
# IO信息
dis_io = psutil.disk_io_counters(perdisk=True)['PhysicalDrive0'] # "per_disk=True",获取单个分区的IO信息
print(dis_io)
print('读取总次数:' + str(dis_io.read_count) + '写入总次数:' + str(dis_io.write_count) + ',读取:' + str(
round(dis_io.read_bytes / unit_gb, 2)) + 'G,写入字节:' + str(round(dis_io.write_bytes / unit_gb, 2)) + 'G,读取时间:' + str(
dis_io.read_time) + '写入时间:' + str(dis_io.write_time)) # 网络信息
print(psutil.net_io_counters())
# 单个接口的信息
print(psutil.net_io_counters(pernic=True)) # 登录用户信息
print('登录用户信息:' + str(psutil.users())) # 获取进程信息
print(psutil.pids()) # 获取所有进程pid
p = psutil.Process(8928)
print(p.name() + ',' + p.exe() + ',' + p.status() + ',' + str(p.cpu_times()))







