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

网友投稿 916 2022-05-28

曾想仗剑走天涯

《哪吒》上映33天,票房45.24亿,观影人次1.25亿,可惜一直没时间去看。

从大圣归来开始,国漫电影就主键火了,当然我说的肯定不包括《喜羊羊与灰太狼》。前阵子还看了《风语咒》,虽然普遍反映动画面瘫,但还是希望国漫之路能走的越来越好吧。

电脑管家

也许大家都有这样的感觉,优化完美的电脑系统,你把电脑借给一个电脑小白使用上几天,等你拿回来的时候会发现,开机各种慢,乱七八糟的软件装了一大堆。那么我们如何使用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=51.2)

查看磁盘的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)

>>> 数据类型:

获取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)

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

>>> 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

本文来自“清风Python”公众号

开发者 编程语言 python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Linux系统之ubuntu系统root登录
下一篇:Java---IO加强(3)-IO流的操作规律
相关文章