Python get last updated file

Python : How to get Last Access & Creation date time of a file

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.

Frequently Asked:

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,

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.

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,

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,

# 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,

Читайте также:  Unable to start tor java io ioexception ошибка

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,

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.

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,

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,

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

Источник

Читайте также:  How to open java class files

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

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 )

Frequently Asked:

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.

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

Читайте также:  Css select active border

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

Источник

Оцените статью