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...