« »
2008-08-21Python

105

Python:获得文件夹大小

  1. import os
  2. def get_size(src):
  3.     '''Get the size of a directory or a file'''
  4.     size=0L
  5.     if os.path.isfile(src):
  6.         size=os.stat(src)[6]
  7.     elif os.path.isdir(src):
  8.         for item in os.listdir(src):
  9.             itemsrc=os.path.join(src,item)
  10.             print item
  11.             #iterate to caculate the directory size
  12.             size+=get_size(itemsrc)
  13.     return size
  14.  
  15. if __name__=='__main__':
  16.  
  17.     dirname=r'c:\work'
  18.     print get_size(dirname)

您还可能感兴趣的内容

日志信息 »

该日志于2008-08-21 16:09由 x72 发表在Python分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »


返回顶部