日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python使用psutil模塊,做你的電腦管家

系統 1688 0

電腦管家

也許大家都有這樣的感覺,優化完美的電腦系統,你把電腦借給一個電腦小白使用上幾天,等你拿回來的時候會發現,開機各種慢,亂七八糟的軟件裝了一大堆。那么我們如何使用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)
>>> 數據類型: 
              
            
          

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 若尔盖县| 抚顺县| 阳泉市| 儋州市| 常熟市| 嵊泗县| 赤峰市| 方正县| 扎囊县| 广丰县| 明光市| 板桥市| 高雄县| 临武县| 甘南县| 黄石市| 措勤县| 米泉市| 白城市| 自治县| 桃园县| 买车| 揭阳市| 威宁| 政和县| 屏山县| 黔西县| 咸阳市| 建平县| 阿尔山市| 本溪市| 界首市| 营口市| 清河县| 兴和县| 堆龙德庆县| 陈巴尔虎旗| 额尔古纳市| 桐乡市| 靖边县| 抚州市|