I would like to visit every directory, subdirectory and count the number of text files in each directory and subdirectory. I want the sample output as Expected Output:
C:\Users\DELL1\Desktop\ABC\Folder1 - 3 files
C:\Users\DELL1\Desktop\DEF - 5 files
C:\Users\DELL1\Desktop\XYZ - 4 files
C:\Users\DELL1\Desktop\PQR\Folder2 - 2 files
below is the program I tried (I am a novice programmer)
import os
extension = '.txt'
for dirpath, dirs, files in os.walk("."):
count = 0
for filename in files:
if filename.endswith('.txt'):
count = count + 1
print(dirpath)
print(count)
I have tried like this but I am getting output in the form given below. Actually, there are only 3 txt files only in ABC\Folder1. But it is showing output like this. How to rectify my code to get the expected output
Kindly help.