Home >php教程 >php手册 >python计算指定路径下所有目录大小的脚本

python计算指定路径下所有目录大小的脚本

WBOY
WBOYOriginal
2016-06-13 09:25:371004browse

python计算指定路径下所有目录大小的脚本

   检测指定目录下文件的大小只需要遍历目录然后再统计文件之后进行总结就可以了,下面来看一个python的检测一个指定路径下,所有目录大小的脚本

  例子

 代码如下  

import os,sys
from os.path import getsize

def lsdir(rootDir):
    list_dirs = os.walk(rootDir)
    size = 0L
    for root,dirs,files in list_dirs:
        for name in files:
            size += getsize(os.path.join(root,name))
    return size


def haveDir(rootDir):
    list_dirs = os.walk(rootDir)
    for root,dirs,files in list_dirs:
        for dir in dirs:
            dirSize = lsdir(os.path.join(root,dir))
            dirSize = int(dirSize)
            print dir,":",dirSize//1000

targetDir = sys.argv[1]
haveDir(targetDir)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn