In this article, we will discuss different ways to get list of all files in a directory / folder sorted by date and time in python.

Table of contents

Get list of files in directory sorted by date using glob()

In python, the glob module provides a function glob() to find files in a directory based on matching pattern. Similar to unix path expansion rules we can use wildcards and regular expression to match & find few or all files in a directory. We will use this to get a list of all files in a directory but sorted by last modification time. Steps are as follows,

  1. Get a list of all files in a directory using glob()
  2. Sort the list of files based on last modification time using sorted() function. For this, use os.path.getmtime() as the key argument in sorted() function.

Complete example to get list of files in directory sorted by last modification datetime is as follows,

import glob
import os
import time


dir_name = 'C:/Program Files/Java/jdk1.8.0_191/'

# Get list of all files only in the given directory
list_of_files = filter( os.path.isfile,
                        glob.glob(dir_name + '*') )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted( list_of_files,
                        key = os.path.getmtime)

# Iterate over sorted list of files and print file path 
# along with last modification time of file 
for file_path in list_of_files:
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_path) 

Output:

10/06/2018 :: 04:34:06  --> C:/Program Files/Java/jdk1.8.0_191\COPYRIGHT      
10/06/2018 :: 04:34:08  --> C:/Program Files/Java/jdk1.8.0_191\src.zip        
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\LICENSE        
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\README.html    
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\THIRDPARTYLICENSEREADME-JAVAFX.txt
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\THIRDPARTYLICENSEREADME.txt
11/18/2018 :: 09:42:12  --> C:/Program Files/Java/jdk1.8.0_191\javafx-src.zip 
11/18/2018 :: 09:42:19  --> C:/Program Files/Java/jdk1.8.0_191\release  

In this solution we created a list of files in a folder sorted by date. But the list contains the complete path of files. What if we want only file names in sorted order of date?

Get list of files in directory sorted by date using os.listdir()

In Python, the os module provides a function listdir(dir_path), which returns a list of file names in the given directory path. Then we can sort this list of file names based on the last modification time using os.path.getmtime() function as the key argument in the sorted() function.

Complete example to get list of files in directory sorted by last modification datetime is as follows,

import os
import time


dir_name = 'C:/Program Files/Java/jdk1.8.0_191/'

# Get list of all files only in the given directory
list_of_files = filter( lambda x: os.path.isfile(os.path.join(dir_name, x)),
                        os.listdir(dir_name) )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted( list_of_files,
                        key = lambda x: os.path.getmtime(os.path.join(dir_name, x))
                        )

# Iterate over sorted list of files and print file path 
# along with last modification time of file 
for file_name in list_of_files:
    file_path = os.path.join(dir_name, file_name)
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_name)    

Output:

10/06/2018 :: 04:34:06  --> COPYRIGHT
10/06/2018 :: 04:34:08  --> src.zip
11/18/2018 :: 09:42:11  --> LICENSE
11/18/2018 :: 09:42:11  --> README.html
11/18/2018 :: 09:42:11  --> THIRDPARTYLICENSEREADME-JAVAFX.txt
11/18/2018 :: 09:42:11  --> THIRDPARTYLICENSEREADME.txt
11/18/2018 :: 09:42:12  --> javafx-src.zip
11/18/2018 :: 09:42:19  --> release

In this solution we created a list of file names in a folder sorted by date.

Python: Get list of files a directory and sub-directories sorted by date

In both the previous examples we created a list of files in a directory sorted by date. But it covered the files only in the given directory only, not in nested directories. So, if you want to get a list of files in directory and sub-directory sorted by date then checkout this example,

import glob
import os
import time


dir_name = 'C:/Program Files/Java/jdk1.8.0_191/'

# Get list of all files only in the given directory
list_of_files = filter( os.path.isfile,
                        glob.glob(dir_name + '/**/*') )

# Sort list of files based on last modification time in ascending order
list_of_files = sorted( list_of_files,
                        key = os.path.getmtime)

# Iterate over sorted list of files and print file path 
# along with last modification date time 
for file_path in list_of_files:
    timestamp_str = time.strftime(  '%m/%d/%Y :: %H:%M:%S',
                                time.gmtime(os.path.getmtime(file_path))) 
    print(timestamp_str, ' -->', file_path)  

Output:

11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\bin\appletviewer.exe
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\bin\extcheck.exe      
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\bin\idlj.exe
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jdwpTransport.h
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jni.h
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jvmti.h
11/18/2018 :: 09:42:11  --> C:/Program Files/Java/jdk1.8.0_191\include\jvmticmlr.h
11/18/2018 :: 09:42:13  --> C:/Program Files/Java/jdk1.8.0_191\jre\COPYRIGHT
11/18/2018 :: 09:42:13  --> C:/Program Files/Java/jdk1.8.0_191\jre\Welcome.html
11/18/2018 :: 09:42:13  --> C:/Program Files/Java/jdk1.8.0_191\lib\ant-javafx.jar
11/18/2018 :: 09:42:14  --> C:/Program Files/Java/jdk1.8.0_191\lib\ct.sym
11/18/2018 :: 09:42:14  --> C:/Program Files/Java/jdk1.8.0_191\lib\dt.jar
11/18/2018 :: 09:42:14  --> C:/Program Files/Java/jdk1.8.0_191\lib\jvm.lib
11/18/2018 :: 09:42:17  --> C:/Program Files/Java/jdk1.8.0_191\lib\orb.idl
11/18/2018 :: 09:42:17  --> C:/Program Files/Java/jdk1.8.0_191\lib\packager.jar
11/18/2018 :: 09:42:17  --> C:/Program Files/Java/jdk1.8.0_191\lib\sa-jdi.jar
11/18/2018 :: 09:42:26  --> C:/Program Files/Java/jdk1.8.0_191\lib\tools.jar

We use the glob() function with pattern ‘/**/*’, it will give a list of all files in given directory and all sub-directories. Then using os.path.getmtime() function as the key argument in the sorted() function, we created a list of files sorted by date and time.

Summary:

We learned about different ways to get a list of files in a folder, sorted by date & time.