Sort files by size in a directory in Python
In this post, I will be explaining how to sort files by size in a directory in Python.
Basic Process/Algorithm for the Task: Sort files by size
- I’ll be using Python’s inbuilt ‘os’ module for the task
import os
- you’ll need to enter the path to the folder where you want to perform the task.
def path_allocation(): dir_path = input('Enter the Path to a directory:\n') if os.path.isdir(dir_path): return dir_path else: dir_path = path_allocation() return dir_path
And call this function from the main method to work with the directory path you have entered.
- In case you enter a wrong path, you’ll be asked to re-enter a correct path until you interrupt the execution or enter the right path.
- For better performance, I’ll first create a dictionary of {filenames: sizes}, then sort the dictionary by the value.
# creating a dictionary with file sizes size_dic = {} for files in os.listdir(directory_location): if os.path.isfile(files): # restricting for the files only size_dic[files] = os.stat(files).st_size # this will give size in bytes
sorting the diction by values, using
key=lambda
# sorting dictionary by size, --> value of the dictionary print(f'\tFile Name{6*" "}\t\tFile Size\n{24*"--"}') for file,size in sorted(size_dic.items(), key = lambda kv:(kv[1], kv[0])): print(f'{file}\t\t----> {size/1000:.03f} Kb') # printing file size and names sorted by their size
Now If I sum up the whole process, the code will look like as below:
import os def path_allocation(): dir_path = input('Enter the Path to a directory:\n') if os.path.isdir(dir_path): return dir_path else: dir_path = path_allocation() return dir_path if __name__ == '__main__': directory_location = path_allocation() # for getting the directory path from the user. os.chdir(directory_location) # changing the current directory to the given path # creating dictionary with file sizes size_dic = {} for files in os.listdir(directory_location): if os.path.isfile(files): # restricting for the files only size_dic[files] = os.stat(files).st_size # this will give size in bytes # sorting dictionary by size, --> value of the dictionary print(f'\tFile Name{6*" "}\t\tFile Size\n{24*"--"}') for file,size in sorted(size_dic.items(), key = lambda kv:(kv[1], kv[0])): print(f'{file}\t\t----> {size/1000:.03f} Kb') # printing file size and names sorted by their size
**Note: os.path(‘file-path’).st_size return file size in bytes, thus to convert them into kilobytes, I have divided by 1000. And used :.03
f to precise the file size up to 3 decimal places only.
The main advantage of this code is that this is a platform-independent code and is really fast to sort a large number of file by their sizes.
And the Output will look like this:
Also read:
Leave a Reply