使用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)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值