The following python code counts the number of total files I have in a directory which contains multiple subdirectories. The result prints the subdirectory name along with the number of files it contains.
How can I modify this so that:
- It only looks for a specific file extension (i.e. "*.shp")
- It provides both the number of ".shp" files in each subdirectory and a final count of all ".shp" files
Here is the code:
import os
path = 'path/to/directory'
folders = ([name for name in os.listdir(path)])
for folder in folders:
contents = os.listdir(os.path.join(path,folder))
print(folder,len(contents))