使用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:\
数据类型:  

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

查看电脑的磁盘使用百分比:

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)
数据类型 
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)
数据类型:  

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

查看电脑磁盘分区(物理分区)的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)}
数据类型: 
第一分区: 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)

你可能感兴趣的:(Python)