python检测磁盘容量和文件数

# -*- coding: UTF-8 -*-

from ctypes import *

import os
import win32file

def GetDiskSize(strDrive):
        sectorsPerCluster, bytesPerSector, numFreeClusters, totalNumClusters = win32file.GetDiskFreeSpace(strDrive)

        #可用空间
        freeSpace = (numFreeClusters * sectorsPerCluster * bytesPerSector)

        #总容量
        totalSpace =(sectorsPerCluster * bytesPerSector * totalNumClusters)
        return freeSpace, totalSpace
        

#1.检测磁盘容量
def DiskSpaceFunc():
        strDrive = raw_input("请输入需检测磁盘(例如C、D、E、F):")
        freeSpace, totalSpace = GetDiskSize(strDrive)
        print "磁盘总容量:" + bytes(totalSpace / 1024 / 1024 / 1024) + "GB"
        return

#2.设置阈值提示
def SetThreHint():
        print "设置阈值提示"
        strDrive = raw_input("请输入需检测磁盘(例如C、D、E、F):")
        threValue = raw_input("请设置阈值(MB):")
        freeSpace, totalSpace = GetDiskSize(strDrive)
        if (freeSpace < int(threValue) * 1024 * 1024) :
                print "磁盘剩余空间小于设定阈值:" + bytes(threValue) + "MB,当前剩余磁盘空间:" + bytes(freeSpace / 1024/ 1024) + "MB"
        else:
                print "当前剩余空间超过设定阈值,当前剩余空间:" + bytes(freeSpace / 1024/ 1024)
                
        return

#统计目录中文件数量
def GetFileCount(strPath):
        nDirsNumber = 0 #路径下文件夹数量
       # num_files = 0 #路径下文件数量(包括文件夹)
        nFileNumber = 0 #路径下文件数量,包括子文件夹里的文件数量,不包括空文件夹

        for root,dirs,files in os.walk(strPath):    #遍历统计
                for each in files:
                        #if each[-2:] == '.o':
                                #print root,dirs,each
                        #print each
                        nFileNumber += 1
                
                #if nFileNumber == 10:
                        #break
                for name in dirs:
                        nDirsNumber += 1
                        #print os.path.join(root,name)

       # for fn in os.listdir(path):
           #     num_files += 1
                #print fn

       # print num_dirs
       # print num_files

        return nFileNumber, nDirsNumber

#3.统计磁盘文件个数
def  DiskFileCount():
        print "统计磁盘文件个数"
        strDrive = raw_input("请输入需检测磁盘(例如C、D、E、F):")

        strPath = strDrive + "\\"
        #strPath = "F:\\"
        nFileNumber, nDirsNumber = GetFileCount(strPath)
        print "当前磁盘文件个数:" + bytes(nFileNumber) + ",文件夹个数:" + bytes(nDirsNumber)
        return

print "欢迎使用"

while True:
        print "请选择所需功能:"
        print "1、检测磁盘容量"
        print "2、设置阀值"
        print "3、统计磁盘文件个数"
        print "4、退出功能"
        strSelFunc = raw_input("请选择:")
        if "1" == strSelFunc:
                DiskSpaceFunc()
        elif "2" == strSelFunc:
                SetThreHint()
        elif "3" == strSelFunc:
                DiskFileCount()
        elif "4" == strSelFunc:
                print "谢谢使用!"
                break
        else:
            print "请输入正确的功能序号!"

        IsBackMemu = raw_input("是否需要返回主菜单(Y/N):")
        if(IsBackMemu.upper() == "N"):
                print "谢谢使用!"
                break

exit()

#path = os.getcwd()    #获取当前路径
#path = "F:/7-Zip/"
path = "F:/"
print path

num_dirs = 0 #路径下文件夹数量
num_files = 0 #路径下文件数量(包括文件夹)
num_files_rec = 0 #路径下文件数量,包括子文件夹里的文件数量,不包括空文件夹
 
for root,dirs,files in os.walk(path):    #遍历统计
        for each in files:
                #if each[-2:] == '.o':
                        #print root,dirs,each
                num_files_rec += 1
        for name in dirs:
                num_dirs += 1
                #print os.path.join(root,name)
 
for fn in os.listdir(path):
        num_files += 1
        #print fn

print num_dirs
print num_files
print num_files_rec

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值