• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5import logging
6import os
7
8
9def GetRecursiveDiskUsage(path):
10  """Returns the disk usage in bytes of |path|. Similar to `du -sb |path|`."""
11
12  def get_size(filepath):
13    try:
14      return os.path.getsize(filepath)
15    except OSError:
16      logging.warning('File or directory no longer found: %s', filepath)
17      return 0
18
19  running_size = get_size(path)
20  if os.path.isdir(path):
21    for root, dirs, files in os.walk(path):
22      running_size += sum(
23          [get_size(os.path.join(root, f)) for f in files + dirs])
24  return running_size
25