Search code examples
python-3.xpython-os

how could this be writtern better/simpler?


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))

Solution

  • 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)