zoukankan      html  css  js  c++  java
  • Python psutil模块使用

      1 import psutil
      2 
      3 # 获取内存信息
      4 mem = psutil.virtual_memory()
      5 total = mem.total / 1024 / 1024 / 1024
      6 used = mem.used / 1024 / 1024 / 1024
      7 print('内存总量:' + str(round(total, 2)) + 'G,已使用:' + str(round(used, 2)) + 'G')
      8 
      9 print(psutil.cpu_times())
     10 # 获取CPU的逻辑个数,默认logical为True
     11 logical_count = psutil.cpu_count()
     12 physical_count = psutil.cpu_count(logical=False)
     13 print('逻辑处理器个数为:' + str(logical_count) + "
    物理处理器个数为:" + str(physical_count))
     14 
     15 # 磁盘信息
     16 print(psutil.disk_partitions())  # 完整磁盘信息
     17 disk_c = psutil.disk_usage('c:\')
     18 disk_d = psutil.disk_usage('d:\')
     19 unit_gb = 1024 * 1024 * 1024
     20 print(
     21     'C盘总容量:' + str(round(disk_c.total / unit_gb, 2)) + 'G,已使用:' + str(round(disk_c.used / unit_gb, 2)) + 'G,未使用:' + str(
     22         round(disk_c.free / unit_gb, 2)) + 'G,使用百分比:' + str(disk_c.percent) + '%')  # 获取分区(参数)的使用情况
     23 print(
     24     'D盘总容量:' + str(round(disk_d.total / unit_gb, 2)) + 'G,已使用:' + str(round(disk_d.used / unit_gb, 2)) + 'G,未使用:' + str(
     25         round(disk_d.free / unit_gb, 2)) + 'G使用百分比:' + str(disk_d.percent) + '%')  # 获取分区(参数)的使用情况
     26 # IO信息
     27 dis_io = psutil.disk_io_counters(perdisk=True)['PhysicalDrive0']  # "per_disk=True",获取单个分区的IO信息
     28 print(dis_io)
     29 print('读取总次数:' + str(dis_io.read_count) + '写入总次数:' + str(dis_io.write_count) + ',读取:' + str(
     30     round(dis_io.read_bytes / unit_gb, 2)) + 'G,写入字节:' + str(round(dis_io.write_bytes / unit_gb, 2)) + 'G,读取时间:' + str(
     31     dis_io.read_time) + '写入时间:' + str(dis_io.write_time))
     32 
     33 # 网络信息
     34 print(psutil.net_io_counters())
     35 # 单个接口的信息
     36 print(psutil.net_io_counters(pernic=True))
     37 
     38 # 登录用户信息
     39 print('登录用户信息:' + str(psutil.users()))
     40 
     41 # 获取进程信息
     42 print(psutil.pids())  # 获取所有进程pid
     43 p = psutil.Process(8928)
     44 print(p.name() + ',' + p.exe() + ',' + p.status() + ',' + str(p.cpu_times()))
     45 
     46 
    View Code
  • 相关阅读:
    python基本数据类型剖析
    常用正则表达式
    python_re模块
    迭代器模式
    状态模式
    备忘录模式
    asp.net 发送邮件
    建造者模式
    抽象工厂模式
    摸板模式与钩子
  • 原文地址:https://www.cnblogs.com/lzyGod/p/5623761.html
Copyright © 2011-2022 走看看