码迷,mamicode.com
首页 > 编程语言 > 详细

Python查询磁盘信息,磁盘剩余可用空间

时间:2020-01-16 18:35:45      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:orm   append   return   https   form   dict   div   for   col   

参考: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查询磁盘信息,磁盘剩余可用空间

标签:orm   append   return   https   form   dict   div   for   col   

原文地址:https://www.cnblogs.com/HugJun/p/12202510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!