title: Python: os.stat().st_size gives different value than du
body: <p>I'm creating a utility that will walk through directories and get the sizes of child directories and files for all directories and store the value. However, the sizes aren't computed correctly. </p>
  
  <p>Here's my class, which automatically recurses through all sub-directories:</p>
  
  <pre><code>class directory:
      '''
      Class that automatically traverses directories
      and builds a tree with size info
      '''
      def __init__(self, path, parent=None):
  
          if path[-1] != '/':
              # Add trailing /
              self.path = path + '/'
          else:
              self.path = path
          self.size = 4096
          self.parent = parent
          self.children = []
          self.errors = []
          for i in os.listdir(self.path):
              try:
                  self.size += os.lstat(self.path + i).st_size
                  if os.path.isdir(self.path + i) and not os.path.islink(self.path + i):
                      a = directory(self.path + i, self)
                      self.size += a.size
                      self.children.append(a)
              except OSError:
                  self.errors.append(path + i)
  </code></pre>
  
  <p>I have a directory of videos that I'm testing this program with:</p>
  
  <pre><code>&gt;&gt;&gt; a = directory('/var/media/television/The Wire')
  &gt;&gt;&gt; a.size
  45289964053
  </code></pre>
  
  <p>However, when I try the same with du, I get</p>
  
  <pre><code>$ du -sx /var/media/television/The\ Wire
  44228824
  </code></pre>
  
  <p>The directories don't contain any links or anything special. </p>
  
  <p>Could someone explain why <code>os.stat()</code> is giving weird size readings?</p>
  
  <p>Platform:</p>
  
  <ul>
  <li>Linux (Fedora 13)</li>
  <li>Python 2.7</li>
  </ul>