Posted By

silviud on 01/22/11


Tagged

python unix linux


Versions (?)

Python recursive get size of directory


 / Published in: Python
 

this will display the size of a directory as if you run du -sb dir

  1. def _total_size(source):
  2. total_size = os.path.getsize(source)
  3. for item in os.listdir(source):
  4. itempath = os.path.join(source, item)
  5. if os.path.isfile(itempath):
  6. total_size += os.path.getsize(itempath)
  7. elif os.path.isdir(itempath):
  8. total_size += self._total_size(itempath)
  9. return total_size

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: god_of_nothing on March 9, 2011

good

You need to login to post a comment.