1. 程式人生 > >使用python獲取電腦的磁碟資訊

使用python獲取電腦的磁碟資訊

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

在PyCharm互動介面中進行如下演示:

檢視電腦的磁碟分割槽

d = psutil.disk_partitions()
print('C盤資訊:',d[0])
print('D盤資訊:',d[1])
print('E盤資訊:',d[2])
print('獲取磁碟欄位:',d[0][0],d[1][0],d[2][0])
print('資料型別:',type(d),'\n')

輸出:

C:\Users\ASUS\venv\untitled\Scripts\python.exe E:/pythonProject/untitled/Public_Other/test_Public_Other/顯示系統IO.py
C盤資訊: sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed')
D盤資訊: sdiskpart(device='D:\\', mountpoint='D:\\', fstype='NTFS', opts='rw,fixed')
E盤資訊: sdiskpart(device='E:\\', mountpoint='E:\\', fstype='NTFS', opts='rw,fixed')
獲取磁碟欄位: C:\ D:\ E:\
資料型別: <class 'list'> 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

檢視電腦的磁碟使用百分比:

p = psutil.disk_usage(d[0][0]) #C盤
print('C盤使用百分比:',p)
p = psutil.disk_usage(d[1][0]) #D盤
print('D盤使用百分比:',p)
p = psutil.disk_usage(d[2][0]) #E盤
print('E盤使用百分比:',p)
print('資料型別',type(p))
p_all = psutil.disk_usage('/')
print('Python所在目錄磁碟使用情況:',p_all,'\n')

輸出:

C盤使用百分比: sdiskusage(total=125139517440, used=71230517248, free=53909000192, percent=56.9)
D盤使用百分比: sdiskusage(total=600122060800, used=471762903040, free=128359157760, percent=78.6)
E盤使用百分比: sdiskusage(total=399268376576, used=207760642048, free=191507734528, percent=52.0)
資料型別 <class 'psutil._common.sdiskusage'>
Python所在目錄磁碟使用情況: sdiskusage(total=399268376576, used=207760642048, free=191507734528, percent=52.0) 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

檢視電腦磁碟的IO計數:

io = psutil.disk_io_counters()
print('磁碟IO:',io)
print('資料型別:',type(io),'\n')

輸出:

磁碟IO: sdiskio(read_count=188773, write_count=99822, read_bytes=4444965888, write_bytes=2584822784, read_time=3073, write_time=297)
資料型別: <class 'psutil._common.sdiskio'> 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

檢視電腦磁碟分割槽(物理分割槽)的IO計數:

f = psutil.disk_io_counters(perdisk=True)
print('分割槽資訊(物理分割槽):',f)
print('資料型別:',type(f))
print('第一分割槽:',f['PhysicalDrive0'])
print('第二分割槽:',f['PhysicalDrive1'])

輸出:

分割槽資訊(物理分割槽): {'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)}
資料型別: <class 'dict'>
第一分割槽: sdiskio(read_count=46892, write_count=3934, read_bytes=1487477248, write_bytes=74489856, read_time=2772, write_time=31)
第二分割槽: sdiskio(read_count=141881, write_count=95888, read_bytes=2957488640, write_bytes=2510332928, read_time=301, write_time=266)
[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')]
sdiskusage(total=125139517440, used=71230517248, free=53909000192, percent=56.9)