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
Python使用psutil模块,做你的电脑管家的更多相关文章
- CentOS7--64安装python的psutil模块
1.以root身份登陆CentOS依次 执行以下命令: wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.g ...
- Python 中psutil 模块的安装
第一步下载psutil 的安装包 网址:https://pypi.python.org 第二步解压 .tar.gz cd psutil- 第三步安装: python setup.py build py ...
- python之psutil模块(获取系统性能数据)
psutil模块 1.介绍 psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等) ...
- python之psutil模块(获取系统性能信息(CPU,内存,磁盘,网络)
一.psutil模块 1. psutil是一个跨平台库(http://code.google.com/p/psutil/),能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等) ...
- python之psutil模块详解(Linux)--小白博客
Python-psutil模块 windows系统监控实例,查询 https://www.cnblogs.com/zhou2019/p/10567282.html 1.简单介绍 psutil是一个跨平 ...
- Linux-Centos7----安装Python的psutil模块插件
# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz # tar zxvf psutil-2.1.3. ...
- [Python监控]psutil模块简单使用
安装很简单 pip install psutil 官网地址为 https://pythonhosted.org/psutil/ (文档上有详细的api) github地址为 https://githu ...
- python之psutil模块
简述 psutil是一个跨平台库(http://code.google.com/p/psutil/) ,能够轻松实现获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系 ...
- python中用psutil模块,yagmail模块监控CPU、硬盘、内存使用,阈值后发送邮件
import yagmailimport psutildef sendmail(subject,contents): #连接邮箱服务器 yag = yagmail.SMTP(user='邮箱名称@16 ...
随机推荐
- 带你上手一款下载超 10 万次的 IDEA 插件
作者 | 倪超(银时) 阿里云开发者工具产品专家 本文整理自 11 月 7 日社群分享,每月 2 场高质量分享,点击加入社群. 导读:Cloud Toolkit 是本地 IDE 插件,帮助开发者更高效 ...
- csps60爆零记
整场考试心态是崩的,T1水题打了半天表,将近两个小时才A掉. T2数据结构题想麻烦了,码了5.1K(数据结构选手) 等到最后一刻我发现T3水题,生无可恋.jpg 然后吃屎地在暴力中输出了下标,(%%% ...
- CSP-S 46 题解
改完题了,就稍写一下题解,顺便反思一下! 其实这次考试挺水的,然而我也挺水的,看了考试结束后的成绩,就吃-*了! T1 set 这个我考试的时候实在是没有想到如何去判断-1,然后我就觉得这神仙题没法解 ...
- HtmlSpanner 使用小结 -- 安卓解析html
如何利用 HtmlSpanner解析 HTML格式 的字符串: 1. GitHub 下载HtmlSpanner项目 https://github.com/NightWhistler/HtmlSpann ...
- LNMP+Redis架构部署
工作机制 L(Linux)N(Nginx)M(Mysql)P(PHP)架构想必大家都知道,LNMP架构主要作用是让前端服务与后端存储以及后端的一下服务进行连接起来,来实现php程序的动态请求. 而今天 ...
- Abp vNext 自定义 Ef Core 仓储引发异常
问题 在使用自定义 Ef Core 仓储和 ABP vNext 注入的默认仓储时,通过两个 Repository 进行 Join 操作,提示 Cannot use multiple DbContext ...
- JS 原生面经从入门到放弃 篇幅较长,建议收藏
前言 是时候撸一波 JS 基础啦,撸熟了,银十速拿 offer; 本文不从传统的问答方式梳理,而是从知识维度梳理,以便形成知识网络; 包括函数,数组,对象,数据结构,算法,设计模式和 http. 函数 ...
- PHP 修改数组中的值
PHP 修改数组中的值 ①.二维数组可以通过 for($i = 0; $i < count(Array()); ++ $i) 这种形式修改 实例代码: // 修改 二维数组中的 name为 Ge ...
- nyoj 733-万圣节派对 (printf("%06d", i))
733-万圣节派对 内存限制:64MB 时间限制:1000ms 特判: No 通过数:5 提交数:7 难度:1 题目描述: 万圣节有一个Party,XadillaX显然也要去凑热闹了.因为去凑热闹的人 ...
- 使用iis反向代理.net core应用程序
.net core 其实是自宿主性质的web应用程序,而不再是web网站,所以.net core是可以直接单独作为系统服务部署.但是实际情况中,为了同个一个端口能支持多个web应用和统一管理,还是应该 ...