1import os
2directory = 'mydirpath'
3
4number_of_files = len([item for item in os.listdir(directory) if os.path.isfile(os.path.join(directory, item))])
5
1import os
2isfile = os.path.isfile
3join = os.path.join
4
5directory = 'mydirpath'
6number_of_files = sum(1 for item in os.listdir(directory) if isfile(join(directory, item)))
7
1def count_files(dir):
2 return len([1 for x in list(os.scandir(dir)) if x.is_file()])
3
1import os
2
3list = os.listdir(dir) # dir is your directory path
4number_files = len(list)
5print number_files
6
1import os
2
3file_count = sum(len(files) for _, _, files in os.walk(r'C:\Dropbox'))
4print(file_count)
5
1def directory(path,extension):
2 list_dir = []
3 list_dir = os.listdir(path)
4 count = 0
5 for file in list_dir:
6 if file.endswith(extension): # eg: '.txt'
7 count += 1
8 return count
9
1import os
2
3onlyfiles = next(os.walk(dir))[2] #dir is your directory path as string
4print len(onlyfiles)
5