Get sorted file names from a directory by creation date in Python

Hello, Coders!! In this Python tutorial, we will learn how to get a sorted list of filenames of a directory or folder based on the creation date and time in Python.

Let’s discuss some modules that need to  import for this tutorial:

sys: This module provides various functions to manipulate or change different parts of the Python runtime environment. It also provides access to some variables used by the interpreter.

os: This module has different functions for interaction with the system OS. It gives a portable way to use OS-dependent functionality. The ‘os‘ and ‘os.path‘ modules are some examples of modules that contain various useful functions for interaction with the file system.

time: The time module is used to handle any time-related operations.

stat: The stat module defines various functions and constants for interpreting the results of different os modules.

Program to get names of the sorted files by creation date in Python

Step1: Import all the required modules and macros for this Python program using import.

import sys, os, time
from stat import S_ISREG, ST_CTIME, ST_MODE
#S_ISREG used for the  interpretation  of the values in a stat-struct
#ST_CTIME used to represent metadata changes on Unix and creation time on Windows that occurred recently.
#ST_MODE contains the file type and mode.

 

Step2: Declare a variable for storing the path of the directory/file.

dirpath = '/Users/CodeSpeedy/Desktop/Pyfile/' #intialize the path with your own system's directory/file path

 

Step3: Get the list of all the files entries of the given directory.

FiEnt = (os.path.join(dirpath, file_name) for file_name in os.listdir(dirpath))

 

Step4: Get the stats of the file entries.

FiEnt = ((os.stat(path), path) for path in FiEnt)

 

Step5: Insert the creation date by leaving only the regular files.

FiEnt = ((stat[ST_CTIME], path)
         for stat, path in FiEnt if S_ISREG(stat[ST_MODE]))

 

Step6: Lastly, print the sorted filenames on the output screen.

for cdate, path in sorted(FiEnt):
    print(time.ctime(cdate), os.path.basename(path))

 

Here is the complete Python program:

import sys, os, time

from stat import S_ISREG, ST_CTIME, ST_MODE

dirpath = 'C:\\Users\\Azure\\AppData\\Local\\Programs\\Python\\Python310\\tcl' #Give your own Directory/file path 

FiEnt = (os.path.join(dirpath, file_name) for file_name in os.listdir(dirpath))
FiEnt = ((os.stat(path), path) for path in FiEnt)
FiEnt = ((stat[ST_CTIME], path) for stat, path in FiEnt if S_ISREG(stat[ST_MODE]))
for cdate, path in sorted(FiEnt): print(time.ctime(cdate), os.path.basename(path))

Output:

Mon Oct 4 19:13:14 2021 tcl86t.lib
Mon Oct 4 19:13:14 2021 tclConfig.sh
Mon Oct 4 19:13:14 2021 tclooConfig.sh
Mon Oct 4 19:13:14 2021 tclstub86.lib
Mon Oct 4 19:13:14 2021 tk86t.lib
Mon Oct 4 19:13:14 2021 tkstub86.lib

Hope this article has helped you, learning how to get the sorted file names from any directory by creation date in Python.

Happy Coding!!!

You can also read, Python Date and Time

Leave a Reply

Your email address will not be published. Required fields are marked *