Python : Get Last Modification date & time of a file. | os.stat() | os.path.getmtime()

In this article we will discuss different ways to get the last modification date & time of a file and how to convert them into different formats.

Get last modification time of a file using os.stat()

os.stat(pathOfFile)

It returns the status of file in the form of an os.stat_result object. It contains information related to a file like it’s mode, link type, access, creation or modification time etc.

To get the last modification time from os.stat_result object access the property ST_MTIME, that contains the time of
most recent file modification in seconds. Then we can covert that to readable format using time.ctime() i.e.

fileStatsObj = os.stat ( filePath )

modificationTime = time.ctime ( fileStatsObj [ stat.ST_MTIME ] )
 
print("Last Modified Time : ", modificationTime )

Output:

Last Modified Time :  Sun Feb 25 15:04:09 2018

Get last modification time of a file using os.path.getmtime()

Python’s os.path module provides an another API for fetching the last modification time of a file i.e.

os.path.getmtime(path)

Here, path represents the path of file and it returns the last modification time of file in terms of number of seconds since the epoch. Then we can convert the time since epoch to different readable format of timestamp. Let’s see an example,

Get last modification time using os.path.getmtime() & time.localtime()

# Get file's Last modification time stamp only in terms of seconds since epoch 
modTimesinceEpoc = os.path.getmtime(filePath)

# Convert seconds since epoch to readable timestamp
modificationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(modTimesinceEpoc))

print("Last Modified Time : ", modificationTime )

Output:

Last Modified Time :  2018-02-25 15:04:09

time.localtime() converts the seconds since epoch to a struct_time in local timezone. Then by passing that time struct to time.strftime() we can get timestamp in readable format.

By changing format string in time.strftime() we can get date only and also in other format specific to our application i.e.

# Convert seconds since epoch to Date only
modificationTime = time.strftime('%d/%m/%Y', time.localtime(os.path.getmtime(filePath)))

print("Last Modified Time : ", modificationTime )

Output:

Last Modified Time :  25/02/2018

Get last modification time using os.path.getmtime() & datetime.fromtimestamp()

Instead of time.localtime() we can also use another function datetime.fromtimestamp() to convert seconds since epoch to time object. Then we can call time.strftime() to convert that to readable format. For example,

modTimesinceEpoc = os.path.getmtime(filePath)

modificationTime = datetime.datetime.fromtimestamp(modTimesinceEpoc).strftime('%Y-%m-%d %H:%M:%S')

print("Last Modified Time : ", modificationTime )

Output:

Last Modified Time :  2018-02-25 15:04:09

Get last modification time of a file in UTC Timezone

To get the last modification time in UTC timezone use datetime.utcfromtimestamp() i.e.

modTimesinceEpoc = os.path.getmtime(filePath)

modificationTime = datetime.datetime.utcfromtimestamp(modTimesinceEpoc).strftime('%Y-%m-%d %H:%M:%S')

print("Last Modified Time : ", modificationTime , ' UTC')

Output:

Last Modified Time :  2018-02-25 09:34:09  UTC

Complete example is as follows,

import os
import stat
import time
import datetime

def main():

    filePath = '/home/varun/index.html'
    
    print("**** Get last modification time using os.stat() ****")
    
    fileStatsObj = os.stat ( filePath )
    
    modificationTime = time.ctime ( fileStatsObj [ stat.ST_MTIME ] )
     
    print("Last Modified Time : ", modificationTime )
    
    print("**** Get last modification time using os.path.getmtime() & time.localtime() ****")
    
    # Get file's Last modification time stamp only in terms of seconds since epoch 
    modTimesinceEpoc = os.path.getmtime(filePath)
    
    # Convert seconds since epoch to readable timestamp
    modificationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(modTimesinceEpoc))
    
    print("Last Modified Time : ", modificationTime )
    
    # Convert seconds since epoch to Date only
    modificationTime = time.strftime('%d/%m/%Y', time.localtime(os.path.getmtime(filePath)))
    
    print("Last Modified Time : ", modificationTime )
    
    print("**** Get last modification time using os.path.getmtime() & datetime.fromtimestamp() ****")
    
    modTimesinceEpoc = os.path.getmtime(filePath)
    
    modificationTime = datetime.datetime.fromtimestamp(modTimesinceEpoc).strftime('%Y-%m-%d %H:%M:%S')
    
    print("Last Modified Time : ", modificationTime )
    
    modificationTime = datetime.datetime.fromtimestamp(modTimesinceEpoc).strftime('%c')
    
    print("Last Modified Time : ", modificationTime )

    print("**** Get last modification time in UTC Timezone ****")

    modTimesinceEpoc = os.path.getmtime(filePath)
    
    modificationTime = datetime.datetime.utcfromtimestamp(modTimesinceEpoc).strftime('%Y-%m-%d %H:%M:%S')
    
    print("Last Modified Time : ", modificationTime , ' UTC')
    
    
    
if __name__ == '__main__':
    main()
    

Output:

**** Get last modification time using os.stat() ****
Last Modified Time :  Sun Feb 25 15:04:09 2018
**** Get last modification time using os.path.getmtime() & time.localtime() ****
Last Modified Time :  2018-02-25 15:04:09
Last Modified Time :  25/02/2018
**** Get last modification time using os.path.getmtime() & datetime.fromtimestamp() ****
Last Modified Time :  2018-02-25 15:04:09
Last Modified Time :  Sun Feb 25 15:04:09 2018
**** Get last modification time in UTC Timezone ****
Last Modified Time :  2018-02-25 09:34:09  UTC

 

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top