Python分析C盘大文件[夹]
2015-09-24 14:53
549 查看
__author__ = 'baron' import os import codecs from os.path import join, getsize def getdirsize(dirDict, rootpath): dirsize = 0L for root, dirs, files in os.walk(rootpath): if root == rootpath: for dir in dirs: dirDict, fsize = getdirsize(dirDict, join(rootpath, dir)) dirsize += fsize try: dirsize += sum([getsize(join(rootpath, file)) for file in files]) except: pass if dirsize / 1024 / 1024 != 0 and rootpath not in dirDict: dirDict[rootpath] = dirsize / 1024 / 1024 print len(dirDict) return dirDict, dirsize if __name__ == '__main__': write_path = 'C:\\Users\\baron\\Desktop\\LOG.txt' write_file = codecs.open(write_path, 'w', encoding='UTF-8') rootpath = 'C:\\' dirDict = {} for file in os.listdir(rootpath): if file.startswith('$'): continue if os.path.isdir(join(rootpath,file)): dirDict, fsize = getdirsize(dirDict, join(rootpath, file)) seq_dict = sorted(dirDict.items(), key=lambda t: t[1], reverse=True) for item in seq_dict: write_file.write('\t'.join([item[0], '%.1f' % item[1]]).decode('gbk')+'\n')
相关文章推荐
- Python 通过print将数据保存到文件中
- Python opencv 调用sift
- sublime配置python运行环境
- 记录python ConfigParser 处理编码问题
- 笨办法学python学习笔记(1~5)
- the basic operation of sequence in python
- Python之分支与循环
- Python之分支与循环
- Python try/except/finally应用
- 学生信息管理后台(python+django+sql)
- slice operation of consequence in python
- 0924_How to Work with Me
- 【Python之旅】第三篇(四):Pythoh模块
- 【Python之旅】第三篇(四):Pythoh模块
- Generate C enum define from python dict
- Python之序列化
- 自定义python startup脚本使交互解释器支持tab和删除键
- Python判断字符串是否符合某一正则表达式
- python torndb使用简介
- Python数组遍历