Get sorted file names from a directory by creation date in Python
Last Updated :
28 Apr, 2025
In this article, we will understand how to retrieve sorted file names from a directory using Python. For this, we would make use of the Python glob library's glob function. There is no need to install this module externally because it is already included with Python.
Firstly, The glob function would be used to obtain the list of files/directories within the specified directory, then we will filter the previous output with the os.path.isfile command which assures that only files are present in the list, not directories/folders.
Syntax of methods used:
In Python, the glob module is used to retrieve files/pathnames matching a specified pattern.
Syntax: glob(pathname, *, recursive=False):
Return: It returns list of path names that match pathname given, which must be a string containing a path specification. List can be empty too.
The os.path.isfile() method in Python is used to check whether the specified path is an existing regular file or not.
Syntax: os.path.isfile(path)
Return: This method returns a Boolean value of class bool. This method returns True if specified path is an existing regular file, otherwise returns False.
The os.path.getctime flag would be used to find the creation timestamps of these files, which would be used to sort them based on their creation time.
Syntax: os.path.getctime(path)
Return: This method returns a floating-point value of class ‘float’ that represents the ctime (in seconds) for the specified path.
The path in which the files exist: C:\Users\apples\images
A directory containing files and folders alongside their timestamps of creation
The following program would give the below file list in the same order. If the above directory is sorted in ascending order based on the timestamp of creation, it would look as follows:
Code Implementation
Firstly the variable named path is used to store the path of the directory on which we want to perform our operations. Then the list of files and folders inside the path was obtained (using the * "asterisk" wildcard) and was filtered to remove the directories from the list (using the filter function). The result of the filter function is converted to list data structure. Then the list data structure is sorted based on the creation time constraint, which sorts the elements (filenames) in the list based on the time they were created in ascending order. In the end, the list is displayed.
Python3
import os
import glob
# Path to the directory where
# the files reside
path = r"C:\Users\applese\images"
# Getting the list of files/directories
# in the specified path Filtering the
# list to exclude the directory names
files = list(filter(os.path.isfile, glob.glob(path + "\*")))
# Sorting file list based on the
# creation time of the files
files.sort(key=os.path.getctime)
# Displaying the sorted list
print(files)
Output:
['C:\\Users\\apples\\images\\Untitled-4.psd', 'C:\\Users\\apples\\images\\mrdqgnyswr541.png',
'C:\\Users\\apples\\images\\maxresdefault.jpg', 'C:\\Users\\apples\\images\\u1sthg151.jpg',
'C:\\Users\\apples\\images\\yes dad i a mwinning.png', 'C:\\Users\\apples\\images\\ss.png',
'C:\\Users\\apples\\images\\unnamed.jpg', 'C:\\Users\\apples\\images\\Screenshot 2020-12-15 161254.png',
'C:\\Users\\apples\\images\\Screenshot 2020-12-16 123348.png', 'C:\\Users\\apples\\images\\sample_text.png',
'C:\\Users\\apples\\images\\properties.png', 'C:\\Users\\apples\\images\\watch.ai',
'C:\\Users\\apples\\images\\made at aposter at my house.ai', 'C:\\Users\\apples\\images\\PRDR3769186715_1',
'C:\\Users\\apples\\images\\Red_Apple.jpg', 'C:\\Users\\apples\\images\\PRDR37691867d15_1.jpg',
'C:\\Users\\apples\\images\\Untitled.png', 'C:\\Users\\apples\\images\\wp2510496.gif',
'C:\\Users\\apples\\images\\test.png', 'C:\\Users\\apples\\images\\PNG.png']
Note:
- If the last print statement is replaced by print("\n".join(files)) the output would appear vertically than horizontally (better for visualizing the effect produced).
- glob.glob doesn't work with filenames containing a period as the first character
Similar Reads
Python - Move Files To Creation and Modification Date Named Directories We all understand how crucial it is to manage files based on their creation and modification dates. So, in this article, we will try to build a Python script that would move all of your files to new directories based on their creation and modification dates. Basically, it will look for directories a
4 min read
Python - Get list of files in directory sorted by size In this article, we will be looking at the different approaches to get the list of the files in the given directory in the sorted order of size in the Python programming language. The two different approaches to get the list of files in a directory are sorted by size is as follows: Using os.listdir(
3 min read
How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
2 min read
Python List All Files In Directory And Subdirectories Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen
5 min read
How to Find Day Name from Date in Python In Python programming, we may frequently need to know which day of the week corresponds to a particular date. This can be useful for a variety of applications, including task scheduling, trend analysis, and showing dates in an easy-to-read style.Get Day Name from Date in PythonTo get the day name fr
3 min read