使用python獲取電腦的磁盤信息

源鏈接:https://blog.csdn.net/Ltinginger/article/details/82799952

使用Python獲取電腦的磁盤信息需要藉助於第三方的模塊psutil,這個模塊需要自己安裝,純粹的CPython下面不具備這個功能。

在PyCharm交互界面中進行如下演示:

查看電腦的磁盤分區:

  1. d = psutil.disk_partitions()

  2. print('C盤信息:',d[0])

  3. print('D盤信息:',d[1])

  4. print('E盤信息:',d[2])

  5. print('獲取磁盤字段:',d[0][0],d[1][0],d[2][0])

  6. print('數據類型:',type(d),'\n')

輸出:

  1. C:\Users\ASUS\venv\untitled\Scripts\python.exe E:/pythonProject/untitled/Public_Other/test_Public_Other/顯示系統IO.py

  2. C盤信息: sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed')

  3. D盤信息: sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed')

  4. E盤信息: sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed')

  5. 獲取磁盤字段: C:\ D:\ E:\

  6. 數據類型: <class 'list'>

查看電腦的磁盤使用百分比:

  1. p = psutil.disk_usage(d[0][0]) #C盤

  2. print('C盤使用百分比:',p)

  3. p = psutil.disk_usage(d[1][0]) #D盤

  4. print('D盤使用百分比:',p)

  5. p = psutil.disk_usage(d[2][0]) #E盤

  6. print('E盤使用百分比:',p)

  7. print('數據類型',type(p))

  8. p_all = psutil.disk_usage('/')

  9. print('Python所在目錄磁盤使用情況:',p_all,'\n')

輸出:

  1. C盤使用百分比: sdiskusage(total=125139517440, used=71230517248, free=53909000192, percent=56.9)

  2. D盤使用百分比: sdiskusage(total=600122060800, used=471762903040, free=128359157760, percent=78.6)

  3. E盤使用百分比: sdiskusage(total=399268376576, used=207760642048, free=191507734528, percent=52.0)

  4. 數據類型 <class 'psutil._common.sdiskusage'>

  5. Python所在目錄磁盤使用情況: sdiskusage(total=399268376576, used=207760642048, free=191507734528, percent=52.0)

查看電腦磁盤的IO計數:

 1.io = psutil.disk_io_counters()

 2.print('磁盤IO:',io)

 3.print('數據類型:',type(io),'\n')

輸出:

 1.磁盤IO: sdiskio(read_count=188773, write_count=99822, read_bytes=4444965888, write_bytes=2584822784,        read_time=3073, write_time=297)

 2.數據類型: <class 'psutil._common.sdiskio'>

查看電腦磁盤分區(物理分區)的IO計數:

  1. f = psutil.disk_io_counters(perdisk=True)

  2. print('分區信息(物理分區):',f)

  3. print('數據類型:',type(f))

  4. print('第一分區:',f['PhysicalDrive0'])

  5. print('第二分區:',f['PhysicalDrive1'])

輸出:

  1. 分區信息(物理分區): {'PhysicalDrive0': sdiskio(read_count=46892, write_count=3934, read_bytes=1487477248, write_bytes=74489856, read_time=2772, write_time=31), 'PhysicalDrive1': sdiskio(read_count=141881, write_count=95888, read_bytes=2957488640, write_bytes=2510332928, read_time=301, write_time=266)}

  2. 數據類型: <class 'dict'>

  3. 第一分區: sdiskio(read_count=46892, write_count=3934, read_bytes=1487477248, write_bytes=74489856, read_time=2772, write_time=31)

  4. 第二分區: sdiskio(read_count=141881, write_count=95888, read_bytes=2957488640, write_bytes=2510332928, read_time=301, write_time=266)

  5. [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')]

  6. sdiskusage(total=125139517440, used=71230517248, free=53909000192, percent=56.9)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章