Home >Backend Development >PHP Tutorial >Python script to calculate the size of all directories under a specified path_PHP tutorial

Python script to calculate the size of all directories under a specified path_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:131070browse

Python script to calculate the size of all directories under a specified path

To detect the size of files in a specified directory, you only need to traverse the directory and then count the files and summarize them. Let’s see below A python script to detect the size of all directories under a specified path

Example

The code is as follows

import os,sys
from os.path import getsize

 代码如下  

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)

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)

http://www.bkjia.com/PHPjc/878457.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/878457.htmlTechArticle
Python script to calculate the size of all directories under a specified path. To detect the size of files in a specified directory, you only need to traverse the directory and then count Just summarize it after the file. Let’s look at one...
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