我的ubuntu空间小的可怜,总共就20G,因为是双系统,vista占了100G,汗。。。就这样将就这,其中/home目录只有可怜的7G多,再加上要在淘宝上买东西,所有又装了个虚拟机,用了3G,接着汗。。。接着,某天系统warning,只有不到2M的可用空间,暴汗。。。已经很省着用了,咋个就这么穷,打开目录想看看哪个东西动了我的硬盘,一打开我的妈哟,加上隐藏文件,大大小小直接看不见大小的文件至少上了30个,更不说里面的东西了。。。我要看那些占了较大空间的文件是哪些不是要挨着点开,受不了这么做,google了一下,写了个python 文件,还是把大家伙找出来了,嘿嘿。。。
import os
from os.path import join,getsize
#得到一个目录下所有文件以及目录的大小,用了递归
def get_size(src):
'''Get the size of a directory or a file'''
size=0L
if os.path.isfile(src): #文件直接算
size=os.stat(src)[6]
elif os.path.isdir(src):
for item in os.listdir(src):
itemsrc=join(src,item)
#print item
#iterate to caculate the directory size
size+=get_size(itemsrc)
return size
#只得到目录的大小,没有用递归
def getdirsize(dir):
size = 0L
for root,dirs,files in os.walk(dir):
size += sum([getsize(join(root, name)) for name in files])
return size
def listdir(dir):
for item in os.listdir(dir):
try:
size = getdirsize(join(dir,item))/1024.0/1024.0
#只打印大于200M的文件
if size>200:
print 'file is %s ,size is %.2f' %(item,size)
except Exception:
continue
if __name__=='__main__':
dirname=r'/home/sunny'
listdir(dirname)
因为ubuntu下可以直接看见该文件夹下的文件的大小,看不见目录的大小,所以直接用第二个方法就可以了,当然这个带点人工,如果要全自动的就用第一个方法。