Python查询磁盘信息,磁盘剩余可用空间
Posted 蜘蛛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python查询磁盘信息,磁盘剩余可用空间相关的知识,希望对你有一定的参考价值。
参考:https://www.iteye.com/blog/halfish-1706810
环境:
Python: 3.6
系统:server 2016
设计模块包:wmi #pip install wmi
import wmi def get_disk_info(): """ 获取物理磁盘信息。 """ tmplist = [] c = wmi.WMI() for physical_disk in c.Win32_DiskDrive(): tmpdict = {} tmpdict["Caption"] = physical_disk.Caption tmpdict["Size"] = int(physical_disk.Size) / 1024 / 1024 / 1024 tmplist.append(tmpdict) return tmplist def get_fs_info(): """ 获取文件系统信息 包含分区的大小、已用量、可用量、使用率、挂载点信息 """ tmplist = [] c = wmi.WMI() for physical_disk in c.Win32_DiskDrive(): for partition in physical_disk.associators("Win32_DiskDriveToDiskPartition"): for logical_disk in partition.associators("Win32_LogicalDiskToPartition"): tmpdict = {} tmpdict["Caption"] = logical_disk.Caption tmpdict["DiskTotal"] = int(logical_disk.Size) / 1024 / 1024 / 1024 tmpdict["UseSpace"] = (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / 1024 / 1024 / 1024 tmpdict["FreeSpace"] = int(logical_disk.FreeSpace) / 1024 / 1024 / 1024 tmpdict["Percent"] = int( 100.0 * (int(logical_disk.Size) - int(logical_disk.FreeSpace)) / int(logical_disk.Size)) tmplist.append(tmpdict) return tmplist if __name__ == "__main__": disk = get_disk_info() print(disk) #获取磁盘信息 fs = get_fs_info() for f in fs: disk_name = f[‘Caption‘] #磁盘名 DiskTotal = f[‘DiskTotal‘] #磁盘大小 单位G disk_UseSpace = f[‘UseSpace‘] #已用磁盘大小 单位G disk_FreeSpace = f[‘FreeSpace‘] #剩余可用磁盘大小 单位G print(‘磁盘名:{} 磁盘大小:{}G 已用空间:{}G 剩余可用空间:{}G !!‘.format(disk_name,round(DiskTotal,2),round(disk_UseSpace,2),round(disk_FreeSpace,2)))
以上是关于Python查询磁盘信息,磁盘剩余可用空间的主要内容,如果未能解决你的问题,请参考以下文章
如何查找 Android 上剩余的可用存储空间(磁盘空间)? [复制]