Get the List of Files in a Directory Sorted by Size Using Python.


Directory contains special nodes in computer paths that hold information about other nested paths but do not contain any information. Since they contain files and other directories, they have some size. Sorting by size means ordering the directories in terms of the space occupied by the directories in the disks. In this article, we will explore several methods, like using the os module, glob module, etc. sorted, lambda functions, etc., to perform the same.

Using the OS and Operator Modules

The os module Python provides a way to interact with the operating System. It offers various functions for operating system−related tasks, including file and directory operations, process management, environment variables, etc. The operator module, on the other hand, provides us the in−built Python operators such as map, filter, sorted, etc. The module provides us with the methods which help to write more concise and clean code.

Example

We first imported the os and the operator modules in Python in the following code. Next, we have defined the directory where the files reside. We also defined all the names of the files in the form of lists. We defined an empty list named files. Next, we iterated over the list containing the file names, and under each iteration, we first checked if the element was a file. If the element is a file, we have accessed the size using the getsize() method. We appended the file name and size to the initialized list and sorted it using the sort method.

import os
import operator

directory = 'files'
file_list = ['file1.txt', 'file2.txt', 'file3.txt']

files = []
for file_name in file_list:
    file_path = os.path.join(directory, file_name)
    if os.path.isfile(file_path):
        file_size = os.path.getsize(file_path)
        files.append((file_name, file_size))

files.sort(key=operator.itemgetter(1))

for file in files:
    print(f"File: {file[0]}, Size: {file[1]} bytes")

Output

File: file1.txt, Size: 12 bytes
File: file2.txt, Size: 26 bytes
File: file3.txt, Size: 56 bytes

Using os And Lambda Function

The lambda function is a function that does not have any name and is intended for small tasks. This has the same role as that of any typical function. But since it has no name, we cannot use this in other parts of the code. This is useful when we quickly apply something over iterable objects, but we are sure we don't need the function elsewhere.

Example

The example below has similar codes as the previous one. The only difference is that we have used the lambda function instead of the operator module to sort the files in the list. We used the sorted method of Python to perform sorting and the lambda function to apply a function to all the list elements.

import os

directory = 'files'
file_list = ['file1.txt', 'file2.txt', 'file3.txt']

files = []
for file_name in file_list:
    file_path = os.path.join(directory, file_name)
    if os.path.isfile(file_path):
        file_size = os.path.getsize(file_path)
        files.append((file_name, file_size))

sorted_files = sorted(files, key=lambda x: x[1])

for file in sorted_files:
    print(f"File: {file[0]}, Size: {file[1]} bytes")

Output

File: file1.txt, Size: 12 bytes
File: file2.txt, Size: 26 bytes
File: file3.txt, Size: 56 bytes

Using Glob And OS

The glob module in Python provides a convenient way to search for files using wildcard patterns. It allows you to match file or directory names with wildcards based on specific patterns or patterns. So we can use the function to retrieve a list of files and paths which matches the pattern. The methods include wildcard patterns such as "*", "?" etc.

Example

We first imported glob and os modules using the import statement in the following code. Next, we defined the directory where the files reside. We took all the names of the files in the form of lists. We checked if the element was a file for all the list elements. Next, we sorted the file depending on the size using the sorted method of Python. We used the iteration technique over the list to print the file's name and size.

import glob
import os

directory = 'files'

files = [(os.path.basename(file), os.path.getsize(os.path.join(directory, file))) for file in ['file1.txt', 'file2.txt', 'file3.txt'] if os.path.isfile(os.path.join(directory, file))]
sorted_files = sorted(files, key=lambda x: x[1])

for file in sorted_files:
    print(f"File: {file[0]}, Size: {file[1]} bytes")

Output

File: file1.txt, Size: 12 bytes
File: file2.txt, Size: 26 bytes
File: file3.txt, Size: 56 bytes

Conclusion

In this article, we have understood how to get the list of files in a directory in a sorted size using Python. Python offers several libraries to deal with the directories and files. We can use the glob, os, etc., to deal with the directories and files. To sort the directories, we can use several other programming techniques in Python, like the lambda function, operator modules, etc.

Updated on: 18-Jul-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements