In this article, we will be looking at the different approaches to get the list of the files in the given directory in the sorted order of size in the Python programming language.
The two different approaches to get the list of files in a directory are sorted by size is as follows:
- Using os.listdir() function
- Using glob() functions
Method 1: Using os.listdir() function
os.listdir() method in Python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then a list of files and directories in the current working directory will be returned.
Syntax: os.listdir(path)
Parameters: path (optional) : path of the directory
Return: This method returns the list of all files and directories in the specified path. The return type of this method is list.
In this method, we will create a list of filenames in a folder sorted by file size. We will pass lambda x: os.stat(os.path.join(dir_name, x)).st_size as the key argument to the sorted() function which will sort the files in directory by size.
Python3
import os name_of_dir = 'dir_path' # Storing list of all files # in the given directory in list_of_files list_of_files = filter ( lambda x: os.path.isfile (os.path.join(name_of_dir, x)), os.listdir(dir_name) ) # Sort list of file names by size list_of_files = sorted ( list_of_files, key = lambda x: os.stat (os.path.join(name_of_dir, x)).st_size) # Iterate over sorted list of file # names and print them along with size one by one for name_of_file in list_of_files: path_of_file = os.path.join(name_of_dir, name_of_file) size_of_file = os.stat(path_of_file).st_size print (size_of_file, ' -->' , name_of_file) |
Output:
366 --> descript.ion 1688 --> readme.txt 3990 --> License.txt 15360 --> Uninstall.exe 48844 --> History.txt 50688 --> 7-zip32.dll 78336 --> 7-zip.dll 108074 --> 7-zip.chm 186880 --> 7zCon.sfx 205824 --> 7z.sfx 468992 --> 7z.exe 581632 --> 7zG.exe 867840 --> 7zFM.exe 1679360 --> 7z.dll
Method 2: Using glob() function
In python programming language we have the glob module which provides a function called glob() which is used to find files or directories in a given directory based on the matching pattern. Using the glob() function we can use wildcards and regular expression to match and find few files in a directory or all files in a directory. In this method, we will use glob() function to get a list of all files in a directory along with the size. The steps are as follows,
First, we will get list of all files in a directory using glob(), then we will sort the list of files based on the size of files using sorted() function.
We will use os.stat(file_path).st_size to fetch the file size from stat object of file. Then we will pass the encapsulated size in a lambda function as the key argument in the sorted() function.
Python3
import glob import os name_of_dir = 'dir_path/' # Storing list of all files (file paths) # in the given directory in list_of_files list_of_files = filter ( os.path.isfile, glob.glob(name_of_dir + '*' ) ) # Sort list of files in directory by size list_of_files = sorted ( list_of_files, key = lambda x: os.stat(x).st_size) # Iterate over sorted list of file names # and print them along with size one by one for path_of_file in list_of_files: size_of_file = os.stat(path_of_file).st_size print (size_of_file, ' -->' , path_of_file) |
Output:
366 --> C:/Program Files/7-Zip\descript.ion 1688 --> C:/Program Files/7-Zip\readme.txt 3990 --> C:/Program Files/7-Zip\License.txt 15360 --> C:/Program Files/7-Zip\Uninstall.exe 48844 --> C:/Program Files/7-Zip\History.txt 50688 --> C:/Program Files/7-Zip\7-zip32.dll 78336 --> C:/Program Files/7-Zip\7-zip.dll 108074 --> C:/Program Files/7-Zip\7-zip.chm 186880 --> C:/Program Files/7-Zip\7zCon.sfx 205824 --> C:/Program Files/7-Zip\7z.sfx 468992 --> C:/Program Files/7-Zip\7z.exe 581632 --> C:/Program Files/7-Zip\7zG.exe 867840 --> C:/Program Files/7-Zip\7zFM.exe 1679360 --> C:/Program Files/7-Zip\7z.dll