Get Directory Listing Sorted by Creation Date in Python



Managing files and directories often requires sorting them by creation date. Python provides various methods to achieve this using built-in modules like 'os' and 'pathlib' libraries. Some common approaches to getting a directory list sorted by creation date are as follows.

Using os.listdir() with sorted() and os.path.getctime()

The os.listdir() function makes a list of items (files and directories) within a given directory. By making use of the sorted() function with a custom key that fetches the creation time of each item. We can sort this list by creation time using os.path.getctime() function.

Example

In the following code, the function takes the directory as input. It returns a list of its contents (files and directories) sorted by their creation time using os.listdir() to get the items in the specified directory.

By using os.path.getctime() function we retrieve the creation time of each item. The sorted() function organizes these items based on their creation times.

import os
def sorted_listing_by_creation_time(directory):
    def get_creation_time(item):
        item_path = os.path.join(directory, item)
        return os.path.getctime(item_path)

    items = os.listdir(directory)
    sorted_items = sorted(items, key=get_creation_time)
    return sorted_items
directory = '/path/to/your/directory'
print(sorted_listing_by_creation_time(directory))

Output

Following is the output for the above code.

['file1.txt', 'file2.txt', 'file3.txt']

Using os.scandir() with sorted() and stat()

The os.scandir() function is more efficient than os.listdir() for getting directory contents. We can use the sorted() function with a custom key that fetches the creation time of each entry by using os.stat().

Example

The following example demonstrates how to list the contents of a directory sorted by their creation date using os.scandir() and sorted() functions. By using a lambda function as the sorting key, the entries are sorted based on their creation times obtained from the stat() method.

import os

def sorted_directory_listing_by_creation_time_with_os_scandir(directory):
    def get_creation_time(entry):
        return entry.stat().st_ctime

    with os.scandir(directory) as entries:
        sorted_entries = sorted(entries, key=get_creation_time)
        sorted_items = [entry.name for entry in sorted_entries]
    return sorted_items

Using pathlib.Path.iterdir() with sorted() and stat()

The pathlib module provides a modern approach to file handling. We can use Path.iterdir() function along with sorted() and stat() functions to list items and sort them by creation date.

Example

In the following example, the code defines a function that accepts a directory and returns its contents sorted by creation time. It retrieves each item's creation time using stat().st_ctime creates a "Path" object for the directory and lists its items. Finally, it sorts these items by their creation times.

from pathlib import Path

def sorted_directory(directory):
    def get_creation_time(item):
        return item.stat().st_ctime

    path_object = Path(directory)
    items = path_object.iterdir()
    sorted_items = sorted(items, key=get_creation_time)
    return [item.name for item in sorted_items]

Using pathlib.Path.glob() with sorted() and stat()

The Path.glob() method from pathlib module allows us to obtain an iterator of items (files and directories) within a given directory. We then go on to use sorted() with a custom key function that gets the creation time of each item using os.stat().

Example

The code uses the "Path" class from "pathlib" to handle file system paths. It takes a directory as input and lists its contents sorted by creation time. A "Path" object is created for the directory, and the "glob()" method retrieves all items. The items are then sorted based on their creation times.

from pathlib import Path
def sorted_directory(directory):
def get_creation_time(item):
    return item.stat().st_ctime

path_object = Path(directory)
items = path_object.glob('*')
sorted_items = sorted(items, key=get_creation_time)
return [item.name for item in sorted_items]
print(sorted_directory(directory))

Output

Following is the output for the above code.

SortedList(['file1.txt', 'file2.txt', 'file3.txt'])
Updated on: 2025-02-18T17:11:10+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements