在有些项目中要检测当前盘符下,空闲区域是否满足软件运行要求,所以要做下检测,源码如下所示:
1. 库
import wmi
import pythoncom
2. 源码
def isNotEnoughSpace(fs_limit=1):
try:
pythoncom.CoInitialize()
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"):
if os.getcwd()[:2] == logical_disk.Caption:
if int(logical_disk.FreeSpace) / 1024 / 1024 / 1024 <= fs_limit:
print(
'{} free space size < {}GB, then auto stop capturing.'.format(logical_disk.Caption,
fs_limit))
self.stop()
return True
break
pythoncom.CoUninitialize()
except:
traceback.print_exc()
return False
此接口默认检测当前盘符下空闲区域是否大于1GB;当然可以扩展检测使用了多少,以及当前分配空间多少,或者查看其它盘符的信息。