zoukankan      html  css  js  c++  java
  • Python检测Windows剩余磁盘空间

    设计模块包:

    wmi   #pip install wmi

    windows 10 64 位,安装成功;

    windows 2008 64位,安装失败。

    Windows Management Instrumentation (WMI)

    A lightweight wrapper around the WMI classes available for all Win32 platforms. These provide a standard way to access system-level information and interdependencies

    官网:

    http://timgolden.me.uk/python/index.html

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

    REF

    https://www.cnblogs.com/HugJun/p/12202510.html

  • 相关阅读:
    HTML新解
    关于EF4.1更新数据后的显示问题PagedList
    SQL2008中的XML字段操作,与命名空间相关
    64位Win7+iis7下发布MVC3 web项目
    蓝屏、异常关机操成的 未能加载文件或程序集“....”或它的某一个依赖项。参数错误。
    FreeWriting_12
    FreeWriting_13
    【转】ACE的构建(VC++6.0环境)
    Freewriting_10
    FreeWriting_16
  • 原文地址:https://www.cnblogs.com/emanlee/p/15801004.html
Copyright © 2011-2022 走看看