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

Getting a list of files in a directory sorted by size is a common task in Python file operations. Python provides several approaches using the os module, glob module, and sorting techniques like lambda functions and the operator module.

Using os.listdir() with Operator Module

The os module provides functions to interact with the operating system, while the operator module offers built-in operators for cleaner code ?

import os
import operator

# Create sample files for demonstration
os.makedirs('files', exist_ok=True)
with open('files/small.txt', 'w') as f:
    f.write('Small file')
with open('files/medium.txt', 'w') as f:
    f.write('This is a medium sized file content')
with open('files/large.txt', 'w') as f:
    f.write('This is a much larger file with more content to demonstrate file size sorting')

directory = 'files'
files = []

for filename in os.listdir(directory):
    file_path = os.path.join(directory, filename)
    if os.path.isfile(file_path):
        file_size = os.path.getsize(file_path)
        files.append((filename, file_size))

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

for filename, size in files:
    print(f"File: {filename}, Size: {size} bytes")
File: small.txt, Size: 10 bytes
File: medium.txt, Size: 33 bytes
File: large.txt, Size: 81 bytes

Using os.listdir() with Lambda Function

Lambda functions provide a concise way to sort files without importing additional modules ?

import os

directory = 'files'
files = []

for filename in os.listdir(directory):
    file_path = os.path.join(directory, filename)
    if os.path.isfile(file_path):
        file_size = os.path.getsize(file_path)
        files.append((filename, file_size))

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

for filename, size in sorted_files:
    print(f"File: {filename}, Size: {size} bytes")
File: small.txt, Size: 10 bytes
File: medium.txt, Size: 33 bytes
File: large.txt, Size: 81 bytes

Using glob Module with Pattern Matching

The glob module allows wildcard pattern matching to find files ?

import glob
import os

directory = 'files'
pattern = os.path.join(directory, '*')

files = []
for file_path in glob.glob(pattern):
    if os.path.isfile(file_path):
        filename = os.path.basename(file_path)
        file_size = os.path.getsize(file_path)
        files.append((filename, file_size))

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

for filename, size in sorted_files:
    print(f"File: {filename}, Size: {size} bytes")
File: small.txt, Size: 10 bytes
File: medium.txt, Size: 33 bytes
File: large.txt, Size: 81 bytes

One-liner with List Comprehension

A more concise approach using list comprehension ?

import os

directory = 'files'

files_with_sizes = sorted(
    [(f, os.path.getsize(os.path.join(directory, f))) 
     for f in os.listdir(directory) 
     if os.path.isfile(os.path.join(directory, f))],
    key=lambda x: x[1]
)

for filename, size in files_with_sizes:
    print(f"File: {filename}, Size: {size} bytes")
File: small.txt, Size: 10 bytes
File: medium.txt, Size: 33 bytes
File: large.txt, Size: 81 bytes

Sorting in Descending Order

To get files sorted from largest to smallest, use reverse=True ?

import os

directory = 'files'
files = [(f, os.path.getsize(os.path.join(directory, f))) 
         for f in os.listdir(directory) 
         if os.path.isfile(os.path.join(directory, f))]

files_desc = sorted(files, key=lambda x: x[1], reverse=True)

for filename, size in files_desc:
    print(f"File: {filename}, Size: {size} bytes")
File: large.txt, Size: 81 bytes
File: medium.txt, Size: 33 bytes
File: small.txt, Size: 10 bytes

Comparison of Methods

Method Module Required Best For
os.listdir() + operator os, operator Readable code
os.listdir() + lambda os only Simple sorting
glob + patterns glob, os Pattern matching
List comprehension os only Concise code

Conclusion

Use os.listdir() with lambda functions for simple file size sorting. For pattern-based file selection, use the glob module. List comprehensions provide the most concise solution for straightforward directory listing and sorting.

Updated on: 2026-03-27T08:25:41+05:30

754 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements