用Python查询目录文件大小
2016-08-19 本文已影响23人
Stevent
查看自己电脑硬盘的文件占用情况
问题:想删除C盘的一些内容,又不知道哪些文件占用的,就自己写了个,查询内容存在csv里,自己排序看看,简单粗暴
import os
from os.path import join, getsize
def getdirsize(dir):
for root, dirs, files in os.walk(dir):
try:
for name in files:
filename = join(root, name)
size = getsize(filename )
print('There are {:.3f} Mbytes in {}'.format(size/1024/1024,filename))
with open(r'size.csv','a+') as f:
f.write('{:.3f},{}\n'.format(size/1024/1024,filename))
except WindowsError:
pass
if __name__ == '__main__':
getdirsize(r'c:\\')