Search code examples
pythoncountdirectorysubdirectory

How to iterate through subdirectories in a directory and count the files in the subdirectories in python


I want to iterate over subdirectories in a directory and get the number of files in each of the subdirectories. My (messy) solution looks like this:

import os, sys
import subprocess

def Count_files_in_subd():

    for root, dirs, files in os.walk("/Path/to/directory", topdown=False):
        for name in dirs:
            print name
            f = os.path.join(root, name)
            os.chdir(f)
            i=0
            for filename in os.listdir(os.getcwd()):
                i+=1
            print i


Count_files_in_subd()

The script first looks at the subdirectories in the directory one after another and than changes to the directory it is "looking at", to count the files within.

It works, but I know that there is a nicer way to do this in python. I looked at different questions on SO, but when I tried most solutions, the number of files for each subdirectory is just one, and I assume that the directory is counted as file. Any help on how to do this nicer would be appreciated.


Solution

  • You could just recode as follows to show the number of files in each folder:

    import os
    
    def Count_files_in_subd():
        for root, dirs, files in os.walk("/Path/to/directory"):
            print "{} in {}".format(len(files), root)
    
    Count_files_in_subd()
    

    root holds the current folder being walked, and files already holds all of the files in that folder, so all you need to count them is len(files).

    dirs holds any folders found in the current folder (which will be accessed next). If you do not wish to visit a certain folder, they can be removed from this list.

    This would give you the following kind of output:

    5 in /Path/to/directory
    10 in /Path/to/directory/folder_1
    3 in /Path/to/directory/folder_1/deeper