# -*- coding:utf-8 -*-
'''
python36 - windows获取磁盘空间
Wmi = wmi.WMI()
ListDisk = Wmi.Win32_LogicalDisk ()
DiskFreeSaceint = int(Disks.FreeSpace)
DiskFreeSaceintFormat = getSizeInNiceString(DiskFreeSaceint)
Disks.Caption
'''
import wmi
def getSizeInNiceString(sizeInBytes):
'''
将字节格式化为字符串 例: 9.9bytes/KB/MB/GB
'''
for (cutoff, label) in [(1024*1024*1024, "GB"),
(1024*1024, "MB"),
(1024, "KB"),
]:
if sizeInBytes >= cutoff:
return "%.1f %s" % (sizeInBytes * 1.0 / cutoff, label)
if sizeInBytes == 1:
return "1 byte"
else:
bytes = "%.1f" % (sizeInBytes or 0,)
return (bytes[:-2] if bytes.endswith('.0') else bytes)