Python使用psutil模块,做你的电脑管家

电脑管家

也许大家都有这样的感觉,优化完美的电脑系统,你把电脑借给一个电脑小白使用上几天,等你拿回来的时候会发现,开机各种慢,乱七八糟的软件装了一大堆。那么我们如何使用Python来获取电脑的相关数据呢?不妨了解下psutil模块!

psutil学习

psutil是一个跨平台库(http://pythonhosted.org/psutil/) 能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap等。目前支持32位和64位的Linux、Windows、OS X、FreeBSD和Sun Solaris等操作系统.

模块安装

使用pip install psutil

查看磁盘分区

import psutil

disks = psutil.disk_partitions()
for disk in disks:
    print(disk)

>>> sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed')
>>> sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed')
>>> sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed')
>>> sdiskpart(device='F:\\', mountpoint='F:\\', fstype='NTFS', opts='rw,fixed')

查看磁盘使用率

import psutil

disks = psutil.disk_partitions()
for disk in disks:
    print(disk.device, psutil.disk_usage(disk.device))
>>> C:\ sdiskusage(total=64428584960, used=39714340864, free=24714244096, percent=61.6)
>>> D:\ sdiskusage(total=107389222912, used=44705517568, free=62683705344, percent=41.6)
>>> E:\ sdiskusage(total=322134831104, used=103709868032, free=218424963072, percent=32.2)
>>> F:\ sdiskusage(total=506249498624, used=259100221440, free=247149277184, percent

查看磁盘的IO

import psutil

io = psutil.disk_io_counters()
print('磁盘IO:', io)
print('数据类型:', type(io), '\n')

>>> 磁盘IO: sdiskio(read_count=169062, write_count=69826, read_bytes=7126855680, write_bytes=2237599744, read_time=741, write_time=163)
>>> 数据类型: <class 'psutil._common.sdiskio'>

获取CPU信息

import psutil

# cpu的完整信息
print(psutil.cpu_times())
# CPU逻辑个数
print(psutil.cpu_count())
# cpu使用率
print(psutil.cpu_percent())

>>> scputimes(user=1148.3389611, system=479.95267660000536, idle=43888.806536699994, interrupt=17.752913799999998, dpc=18.345717599999997)
>>> 4
>>> 3.5

获取内存信息

import psutil

mem = psutil.virtual_memory()
print(mem)
print(mem.total/1024/1024)
print(mem.total)
print(mem.used)
print(mem.free)

>>> svmem(total=8478351360, available=4468076544, percent=47.3, used=4010274816, free=4468076544)
>>> 8085.5859375
>>> 8478351360
>>> 4010274816
>>> 4468076544

获取开机时间

import psutil
from datetime import datetime

print(psutil.boot_time())
print(datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H: %M: %S"))

>>> 1566915328.0
>>> 2019-08-27 22: 15: 28

查看系统进程信息

import psutil

for pid in psutil.pids():
    p = psutil.Process(pid)
    print(p.name())
    print(p.as_dict())

>>> python.exe
>>> {'exe': 'D:\\Python37\\python.exe', 'memory_full_info': None, 'ionice': ...
>>> chrome.exe
>>> {'exe': 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe' ...
>>> notepad++.exe
>>> {'exe': 'F:\\Software\\Notepad++\\notepad++.exe', 'memory_full_info': None, ...

The End

OK,今天的内容就到这里,如果觉得内容对你有所帮助,欢迎点赞。
期待你关注我的公众号清风Python,如果觉得不错,希望能动动手指转发给你身边的朋友们。

作者:清风Python

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值