-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_folder_with_many_files.py
31 lines (26 loc) · 1.05 KB
/
get_folder_with_many_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
def list_files(root_path):
files = []
for dirpath, dirnames, filenames in os.walk(root_path):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
if os.path.isfile(filepath):
files.append(filepath)
return files
def get_folders_with_many_files(root_path, threshold, level=3):
folders = {}
for dirpath, dirnames, filenames in os.walk(root_path):
# check if current directory is from this level
if dirpath.count(os.sep) == level:
num_files = len(list_files(dirpath))
# check if directory has more than threshold files
if num_files > threshold:
folders[dirpath] = num_files
return folders
root_path = "./"
threshold = 3000
level=3
folders_with_many_files = get_folders_with_many_files(root_path, threshold, level=level)
print("List of files with more than " + str(threshold) + " files on level " + str(level) + ":")
for key, value in folders_with_many_files.items():
print(key + ": " + str(value))