Python recursive get size of directory

/ Published in: Python
Save to your folder(s)

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

Copy this code and paste it in your HTML
  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


RSS Icon Subscribe to comments

You need to login to post a comment.