• 统计windows文件夹下文件大小(python脚本和powershell脚本)


    import os

    totalSize = 0
    fileNum = 0
    dirNum = 0


    def visitDir(path):
    global totalSize
    global fileNum
    global dirNum
    for lists in os.listdir(path):
    sub_path = os.path.join(path, lists)
    print(sub_path)
    if os.path.isfile(sub_path):
    fileNum = fileNum+1 # 统计文件数量
    totalSize = totalSize+os.path.getsize(sub_path) # 文件总大小
    elif os.path.isdir(sub_path):
    dirNum = dirNum+1 # 统计文件夹数量
    visitDir(sub_path) # 递归遍历子文件夹


    def sizeConvert(size): # 单位换算
    K, M, G = 1024, 1024**2, 1024**3
    if size >= G:
    return str(size/G)+'G Bytes'
    elif size >= M:
    return str(size/M)+'M Bytes'
    elif size >= K:
    return str(size/K)+'K Bytes'
    else:
    return str(size)+'Bytes'


    def main(path):
    if not os.path.isdir(path):
    print('Error:"', path, '" is not a directory or does not exist.')
    return
    visitDir(path)

    def output(path):
    print('The total size of '+path+' is:'+sizeConvert(totalSize))
    print('The total number of files in '+path+' is:',fileNum)
    print('The total number of directories in '+path+' is:',dirNum)


    if __name__ == '__main__':
    path = r'C:zabbix'
    main(path)
    output(path)

    --------------------powershell脚本-------------------------------------------------------

    Write-Host DU 1.0 - 统计目录大小的脚本,作用和linux的du类似。`n
    $args = "C:zabbix"
    if (!$args)
    {write-host "du 绝对目录名,如:`ndu.ps1 d:/mp3"}
    elseif (!(Test-Path $args))
    {write-host "错误:找不到目标目录名!"}
    else
    # 我在前人基础上整理,简化了不必要的功能,效果不错。个人感觉比sysinternals的du.exe好。
    {
    $b=Get-ChildItem $args -Recurse | Measure-Object -property length -sum
    write-host "----【$args -- ",("{0:N2}" -f ($b.sum / 1MB)),"MB】----"
    # 2010-8-15日出炉
    $a=Get-ChildItem $args | Where-Object {$_.PsIsContainer -eq $true}
    #foreach ($i in $a) 列出每个子目录大小
    #{
    #$subFolderItems = (Get-ChildItem $i.FullName -Recurse | Measure-Object -property length -sum)
    #$i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum / 1MB) + " MB"
    #}
    }

  • 相关阅读:
    手写token解析器、语法解析器、LLVM IR生成器(GO语言)
    Windows下切分文件(GnuWin32)
    转载:教你分分钟搞定Docker私有仓库Registry
    marathon传参一
    DC/OS安装
    自己写编程语言-m语言
    揽货最短路径解决方案算法
    揽货最短路径解决方案算法
    用keras作CNN卷积网络书本分类(书本、非书本)
    用keras做SQL注入攻击的判断
  • 原文地址:https://www.cnblogs.com/share-wu/p/10524485.html
Copyright © 2020-2023  润新知