Count all files in all folders/subfolders with Python
<p>Which is the <strong>most efficient way</strong> to count all files in all folders and subfolders in Python? I want to use this on Linux systems.</p>

<p>Example output:</p>

<blockquote>
  <p>(Path files)</p>
  
  <p>/ 2</p>
  
  <p>/bin 100</p>
  
  <p>/boot 20</p>
  
  <p>/boot/efi/EFI/redhat 1</p>
  
  <p>....</p>
  
  <p>/root 34</p>
  
  <p>....</p>
</blockquote>

<p>Paths without a file should be ignored.</p>

<p>Thanks.</p>

<p>You can do it with <code>os.walk()</code>;</p>

<pre><code>import os

for root, dirs, files in os.walk('/some/path'):
    if files:
        print('{0} {1}'.format(root, len(files)))
</code></pre>

<p>Note that this will also include hidden files, i.e. those that begin with a dot (<code>.</code>).</p>

python linux file count