Python - Get list of files in directory sorted by size
Last Updated :
28 Jul, 2021
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:
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
Similar Reads
Python - Get list of files in directory with size In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module. OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a
9 min read
Python - List Files in a Directory Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python.Table of ContentWhat is a Directory in Python?How to List Files in a Directory in PythonLi
8 min read
Python List All Files In Directory And Subdirectories Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen
5 min read
List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type
3 min read
Get sorted file names from a directory by creation date in Python In this article, we will understand how to retrieve sorted file names from a directory using Python. For this, we would make use of the Python glob library's glob function. There is no need to install this module externally because it is already included with Python. Firstly, The glob function would
3 min read