首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

经过python获取目录的大小

2012-11-01 
通过python获取目录的大小通过python获取目录的大小?需要用到的module: os, os.pathimport os??用到的几

通过python获取目录的大小

通过python获取目录的大小

?

需要用到的module: os, os.path

import os;

?

?

用到的几个方法:

?

os.path.exists(strPath): 判断strPath是否存在。

os.path.isfile(strPath): 判断strPath 是否是文件

os.walk(strPath):遍历strPath.返回一个3元组 根目录,目录列表,文件列表。

os.path.getsize(strPath): 获取文件的大小

?

PS:源代码

#!/usr/bin/env pythonimport sys;import os;def GetPathSize(strPath):    if not os.path.exists(strPath):        return 0;    if os.path.isfile(strPath):        return os.path.getsize(strPath);    nTotalSize = 0;    for strRoot, lsDir, lsFiles in os.walk(strPath):        #get child directory size        for strDir in lsDir:            nTotalSize = nTotalSize + GetPathSize(os.path.join(strRoot, strDir));        #for child file size        for strFile in lsFiles:            nTotalSize = nTotalSize + os.path.getsize(os.path.join(strRoot, strFile));    return nTotalSize;if __name__ == '__main__':    nFileSize = GetPathSize(sys.argv[1])    print(nFileSize);
?

结果:

47678506181

==> 得到的大小是byte ??


热点排行