Python检测Windows剩余磁盘空间

参考:

http://linuxshow.blog.51cto.com/1572053/980648

http://bbs.pythonfan.org/thread-2350-1-1.html

 

目标:查看Windows剩余磁盘空间

环境:window XP, python3.2

python官方地址:http://www.python.org/

安装:pywin32-217.win32-py3.2.exe v盘下载地址:http://vdisk.weibo.com/s/gJaR9

官方地址:http://sourceforge.net/projects/pywin32/

安装:WMI-1.4.6.win32.exe V盘下载地址: http://vdisk.weibo.com/s/gJahi

官方地址:http://pypi.python.org/pypi/WMI

 

 

 

*- coding:utf-8-*-

#import

import os,sys
import time
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)
    w= '11111111'
    print(w)
    fs =get_fs_info()
    print(fs)
 


以上代码根据参考代码而来,因为python3与python2不同进行了修改:

print 改为 print()

long() 改为了int()

已测试通过。

另,在python3.3上安装pywin32-217.win32-py3.3.exe不成功,报如下错误:

 

 

Traceback (most recent call last):
File "", line 612,
in File "", line 322,
in install File "", line 160,
in LoadSystemModule File "D:\Python33\lib\imp.py", line 158,
in load_module raise ValueError(msg)ValueError: file object required for import (type code 3)

 没有继续查找原因。

 

 

 

你可能感兴趣的:(python,开发,python3.2,wmi,pywin32,磁盘空间,windows)