ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-09-10 14:35:24  阅读:299  来源: 互联网

标签:exe Python free psutil 模块 print import disk


电脑管家

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

标签:exe,Python,free,psutil,模块,print,import,disk
来源: https://blog.csdn.net/devcloud/article/details/100698239

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有