Python使用psutil模塊,做你的電腦管家
曾想仗劍走天涯
《哪吒》上映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)
>>> 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小時內刪除侵權內容。