Home | History | Annotate | Download | only in utils
      1 # Copyright 2014 The Chromium Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import logging
      6 import os
      7 
      8 
      9 def GetRecursiveDiskUsage(path):
     10   """Returns the disk usage in bytes of |path|. Similar to `du -sb |path|`."""
     11   def get_size(filepath):
     12     try:
     13       return os.path.getsize(filepath)
     14     except OSError:
     15       logging.warning('File or directory no longer found: %s', filepath)
     16       return 0
     17 
     18   running_size = get_size(path)
     19   if os.path.isdir(path):
     20     for root, dirs, files in os.walk(path):
     21       running_size += sum([get_size(os.path.join(root, f))
     22                            for f in files + dirs])
     23   return running_size
     24