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()

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,

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.

Contents of accessTime in string will be,

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.

Contents of creationTime in string will be,

Module required,

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.

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,

Contents of last access time in string will be,

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.

Contents of accessTime in string will be,

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.

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,

Contents of creationTime in string will be,

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

Contents ofcreationTime in string will be,

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

Complete example is as follows,

 

Output:

 

 

Click Here to Subscribe for more Articles / Tutorials like this.