-1

so the goal is to find the folder in a directory tree that has the greatest number of files and the folder that uses the most disk space.

import os

path = 'C:\\animal'
highestsize = ''
mostfile = ''
totalsize = 0
totalfile = 0

for i in os.listdir(path):
    if not os.path.isfile(path + '\\' + i):
        count = 0
        count_file = 0

        for dirpath, subfolder, filenames in os.walk(path + '\\' + i):
            for file in filenames:
                count_file += 1
                count += os.path.getsize(os.path.join(dirpath, file))

        if count_file > totalfile:
            totalfile = count_file
            mostfile = i

        if count > totalsize:
            totalsize = count
            highestsize = i

print(highestsize, str(totalsize) + ' Byte')
print(mostfile + ' have the most files: ' + str(totalfile))
dimas28
  • 1
  • 1
  • 1
    This is [off-topic](https://stackoverflow.com/help/on-topic) for Stack Overflow. As this is working code, you can instead post to https://codereview.stackexchange.com/ for advice on ways to improve it, but make sure to comply with their [on-topic](https://codereview.stackexchange.com/help/on-topic) rules. – costaparas Feb 14 '21 at 02:38
  • oh sorry i didnt know that, i'll do better next time @costaparas – dimas28 Feb 14 '21 at 02:53

1 Answers1

0

I setup a dummy folder with sub-folders and files as below.

### Root folder with 3 sub folders, each with some files.
# root/
#    sub1/
#    sub2/
#    sub3/

Now you can try this to get file counts and folder sizes -

path = '/Users/akshay/Desktop/root'

folder_file_counts = {r:len([r+'/'+files for files in f]) for r,d,f in os.walk(path)}
folder_sizes_bytes = {r:sum([os.stat(r+'/'+files).st_size for files in f]) for r,d,f in os.walk(path)}

print('File counts:', folder_file_counts)
print('Folder sizes:', folder_sizes_bytes)
File counts:
{'/Users/akshay/Desktop/root/sub1': 3,
 '/Users/akshay/Desktop/root/sub2': 3,
 '/Users/akshay/Desktop/root/sub3': 2}

Folder sizes:
{'/Users/akshay/Desktop/root/sub1': 956211,
 '/Users/akshay/Desktop/root/sub2': 643622,
 '/Users/akshay/Desktop/root/sub3': 324885}

Now you can get the max counts/files etc or any other operation you want to do on these dictionaries.


Here is a slightly more efficient code if you have way too many files and folders -

path = '/Users/akshay/Desktop/root'

folder_file_counts = []
folder_sizes_bytes = []

#Loop through the subfolders and files
filepaths = []
for r,d,f in os.walk(path):
    l = []
    s = []
    for fname in f:
        l.append(r+'/'+fname)
        s.append(os.stat(r+'/'+fname).st_size)
    folder_file_counts.append((r,len(l)))
    folder_sizes_bytes.append((r,sum(s)))
    
folder_file_counts = dict(folder_file_counts)
folder_sizes_bytes = dict(folder_sizes_bytes)

print('File counts:', folder_file_counts)
print('Folder sizes:', folder_sizes_bytes)
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • glad to help anytime, do mark the answer if it helped you and do upvote it if it’s well written to your liking. This encourages me to help solve your future questions :) – Akshay Sehgal Feb 14 '21 at 02:50