
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you get a directory listing sorted by creation date in Python?
When working with files in Python, it's often useful to sort them based on their creation time. For example, to find the most recently added file or to process files in the order they were created then we need sort them by the creation date. Python provides built-in methods such as os and os.path which makes it easy to retrieve file metadata and sort directory contents accordingly.
In this article, we will explore all the different method to sort the directories based on the creation date in Python -
- Using os.listdir() with sorted() and os.path.getctime()
- Using os.scandir() with sorted() and stat()
- Using pathlib.Path.iterdir() with sorted() and stat()
- Using pathlib.Path.glob() with sorted() and stat()
Using os.listdir() with sorted() and os.path.getctime()
The os.listdir() function makes a list of items such as 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
Following is the example in which we use os.listdir() to get the items in the specified directory and 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))
Following is the output for the above program -
['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
Here is the example which shows how to list the contents of a directory sorted by their creation date using os.scandir() and sorted() functions -
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 is used to perform file handling operations. We can use Path.iterdir() function along with sorted() and stat() functions to list items and sort them by creation date.
Example
Below is an example in which we use the pathlib.path.iterdir() function, which accepts a directory path and stat().st_ctime, which creates a "Path" object for the directory and lists its items -
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]
The above program returns the sorted directories list based on the creation date -
Using pathlib.Path.glob() with sorted() and stat()
The Path.glob() method from pathlib module is used to obtain an iterator of items i.e., files and directories within a given directory. The sorted() function is used to sort the files based on the creation date using os.stat() method.
Example
Following is an example, which sorts the list of directories based on the creation date by using the method path.glob() with functions sorted() and stat().
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))
Here is the output for the above program -
SortedList(['file1.txt', 'file2.txt', 'file3.txt'])