About Listing Files in a Directory in Python

To get the list of files in a directory sorted by name alphabetically use this:

import os
import glob
from django.conf import settings

pattern = os.path.join(settings.MEDIA_ROOT, "profiles", "*")
for file_path in sorted(glob.glob(pattern)):
    print(file_path)

Tips and Tricks Programming Django 5.x Django 4.2 Django 3.2 Python 3