Home > database >  need to find the number of files in folders and sub folders except .dat files
need to find the number of files in folders and sub folders except .dat files

Time:11-01

I need to find the number of files in folders and sub folders except .dat files, here is my code:

import os

directoryPath = r'C:\Users\\vishns\Documents\Python_Code\Test_Folder'

number_of_files = sum(
    len(files) for r, d, files in os.walk(directoryPath)
)

print(number_of_files)

CodePudding user response:

Here is a solution:

import os

nfiles = 0
ndat = 0
directory = "C://Users//vishns//Documents//Python_Code//Test_Folder"

for root, dirs, files in os.walk(directory):
    for file in files:
        if file.endswith(".dat"):
            ndat  = 1
        else:
            nfiles  = 1
        
print('Number of dat files:{}'.format(ndat))
print('Number of relevant files:{}'.format(nfiles))
print('Number of total files:{}'.format(ndat nfiles))
  • Related