Getting the Last Modification date & time of a file. | os.stat() | os.path.getmtime() in Python.
We will see how we can get the modification date & time of a file and in our desired formats.
Get last modification time of a file using os.stat( ) :
Syntax-os.stat(filePath)
The above function returns os.stat_result that contains stats about the file .
To get the modification time, we have to use the ST_MTIME that will provide us with the modification time in seconds. We will pass it to a function time.ctime( ) which will return the info in a readable manner.
import os
import time
import stat
#Path of the file
fileStats = os.stat ( 'file.txt' )
#Passing the stats so that we get it in a readable manner
modTime = time.ctime ( fileStats [ stat.ST_MTIME ] )
print("Modified Time : ", modTime )
Output : Modified Time : Thu May 13 19:02:47 2021
Get last modification time of a file using os.path.getmtime() :
We can also use another one of python’s os module function i.e. os.path.getmtime( )
Syntax-os.path.getmtime( filePath)
The function returns the number of seconds elapsed after the modification of the file .We have to convert it into a proper format.
Get last modification time using os.path.getmtime() & time.localtime( ) :
import os
import time
#Path of the file going inside the function that returns the number of seconds elpased since modification
modTimeInSeconds = os.path.getmtime ( 'file.txt' )
#Passing the stats so that we get the modification time in a readable manner
modTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(modTimeInSeconds))
print("Modified Time : ", modTime )
Output: Modified Time : 2021-05-13 19:02:47
The function time.localtime( ) converts the seconds to a strruct_time which when passed into strftime( ) returns the timestamp in readable format.
Also we can set the format in strftime( ) to get only the modification date.
import os
import time
#Path of the file going inside the function that returns the number of seconds elpased since modification
modTimeInSeconds = os.path.getmtime ( 'file.txt' )
#Passing the stats so that we get the modification time in a readable manner
modTime = time.strftime('%d/%m/%Y',time.localtime(modTimeInSeconds))
print("Modified Time : ", modTime )
Output : Modified Time : 13/05/2021
Get last modification time using os.path.getmtime() & datetime.fromtimestamp() :
We can also find the modification time of the file without using time.localtime() using datetime.fromtimestamp().
import os
import time
import datetime
#Path of the file going inside the function that returns the number of seconds elpased since modification
modTimeInSeconds = os.path.getmtime ( 'file.txt' )
#Passing the stats so that we get the modification time in a readable manner
modTime = datetime.datetime.fromtimestamp(modTimeInSeconds).strftime('%Y-%m-%d %H:%M:%S')
print("Modified Time : ", modTime )
Output : Modified Time : 2021-05-13 19:02:47
Get last modification time of a file in UTC Timezone :
To obtain the last modification time, we can use datetime.utcfromtimestamp( )
import os
import time
import datetime
#Path of the file going inside the function that returns the number of seconds elpased since modification
modTimeInSeconds = os.path.getmtime ( 'file.txt' )
#Passing the stats so that we get the modification time in a readable manner
modTime = datetime.datetime.utcfromtimestamp(modTimeInSeconds).strftime('%Y-%m-%d %H:%M:%S')
print("Modified Time : ", modTime )
Output : Modified Time : 2021-05-13 13:32:47