In this article we will discuss different ways to get the last access and creation timestamp of a file and how to convert them into different formats.

os.stat()

Python’s os module provides a function os.stat()

os.stat(pathOfFile)

It accepts the path of file as argument and returns the status of file in the form of an os.stat_result object. It contains many information related to the file like it’s mode, link type, access or modification time etc.

Module required,

import os
import stat

Get Last Access time of a file using os.stat()

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

# get the the stat_result object
fileStatsObj = os.stat ( filePath )

# Get last access time
accessTime = time.ctime ( fileStatsObj [ stat.ST_ATIME ] )

Contents of accessTime in string will be,
Sun Oct 21 10:10:40 2018

Get Creation time of a file using os.stat()

To get the creation time from os.stat_result object access the property ST_CTIME. Information it provides is platform dependent i.e.

On Windows:

  • It contains the creation time of file in seconds.

On Unix:

  • It contains the most recent content modification of file in seconds.

Then we can covert that to readable format using time.ctime i.e.

# get the the stat_result object
fileStatsObj = os.stat ( filePath )

# Get the file creation time
creationTime = time.ctime ( fileStatsObj [ stat.ST_CTIME ] )

Contents of creationTime in string will be,
Sun Oct 21 10:10:40 2018

Module required,
import time

Get File Last Access time using os.path.getatime()

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

os.path.getatime(path)

Here, path represents the path of file and it returns the last access time of file in terms of number of seconds since the epoch. Then we can convert the times since epoch to different readable format of timestamp. Let’s see an example,
# Get last access time of file in seconds since epoch
accessTimesinceEpoc = os.path.getatime(filePath)

# convert time sinch epoch to readable format
accessTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(accessTimesinceEpoc))

Contents of last access time in string will be,
2018-10-21 10:10:40

Here, 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.

We can also get the last access time in UTC timezone using time.gmtime() instead of time.localtime() i.e.

accessTime = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(accessTimesinceEpoc))

Contents of accessTime in string will be,
2018-10-21 04:40:40 UTC

Get File creation time using os.path.getctime()

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

os.path.getctime(path)

Here, path represents the path of file and information it returns is platform dependent i.e.

On Windows:

  • It returns the number of seconds since epoch to file creation time.

On Unix:

  • It returns the number of seconds since epoch to time of last metadata change of file.

Then we can convert the times since epoch to different readable format of timestamp. Let’s see an example,

# Get file creation time of file in seconds since epoch
creationTimesinceEpoc = os.path.getctime(filePath)
# convert time sinch epoch to readable format
creationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(creationTimesinceEpoc))

Contents of creationTime in string will be,
2018-10-21 10:10:40

time.localtime() converts the seconds since epoch to a struct_time in local timezone and time.strftime() converts time struct to a readable format provided.

Get File creation time using os.path.getctime() in UTC Timezone

creationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(creationTimesinceEpoc))

Contents ofcreationTime in string will be,
2018-10-21 04:40:40 UTC

time.gmtime() converts the seconds since epoch to a struct_time in UTC timezone.

Complete example is as follows,

import os
import stat
import time

def main():

    filePath = '/home/varung/index.html'

    print("**** Get File Last Access time using os.stat() ****")

    # get the the stat_result object
    fileStatsObj = os.stat ( filePath )

    # Get last access time
    accessTime = time.ctime ( fileStatsObj [ stat.ST_ATIME ] )

    print("File Last Access Time : " + accessTime)

    print("**** Get File Creation time using os.stat() *******")

    # get the the stat_result object
    fileStatsObj = os.stat ( filePath )

    # Get the file creation time
    creationTime = time.ctime ( fileStatsObj [ stat.ST_CTIME ] )

    print("File Creation Time : " + creationTime)

    print("**** Get File Last Access time using os.path.getatime() ****")

    # Get last access time of file in seconds since epoch
    accessTimesinceEpoc = os.path.getatime(filePath)

    # convert time sinch epoch to readable format
    accessTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(accessTimesinceEpoc))

    print("File Last Access Time : " + accessTime)


    print("**** Get File Last Access time using os.path.getatime() in UTC Timezone****")

    accessTime = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(accessTimesinceEpoc))

    print("File Last Access Time : " + accessTime + ' UTC'  )


    print("**** Get File creation time using os.path.getctime() ****")

    # Get file creation time of file in seconds since epoch
    creationTimesinceEpoc = os.path.getctime(filePath)
    # convert time sinch epoch to readable format
    creationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(creationTimesinceEpoc))

    print("File Creation Time : " + creationTime )

    print("**** Get File creation time using os.path.getctime() in UTC Timezone ****")

    creationTime = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(creationTimesinceEpoc))

    print("File Creation Time : ", creationTime , ' UTC'  )

if __name__ == '__main__':
    main()

 

Output:

**** Get File Last Access time using os.stat() ****
File Last Access Time : Sun Oct 21 10:10:40 2018
**** Get File Creation time using os.stat() *******
File Creation Time : Sun Oct 21 10:10:40 2018
**** Get File Last Access time using os.path.getatime() ****
File Last Access Time : 2018-10-21 10:10:40
**** Get File Last Access time using os.path.getatime() in UTC Timezone****
File Last Access Time : 2018-10-21 04:40:40 UTC
**** Get File creation time using os.path.getctime() ****
File Creation Time : 2018-10-21 10:10:40
**** Get File creation time using os.path.getctime() in UTC Timezone ****
('File Creation Time : ', '2018-10-21 04:40:40', ' UTC')