python count files directory code example
Example 1: python count files directory
import os
len(os.listdir(directory))
Example 2: python count files directory
def countFiles(*paths):
"""
:param paths: list of all paths, the number of files are added
:return: return the number of files (not directories) in the folders recursively (subfolders are checked as well)
"""
def helper(path: str):
count = 0
for fileName in os.listdir(path):
filePath = join(path, fileName)
if isfile(filePath):
count += 1
elif isdir(filePath):
count += helper(filePath)
return count
numFiles = 0
for path in paths:
numFiles += helper(path)
return numFiles