{"id":6474,"date":"2021-05-22T09:06:26","date_gmt":"2021-05-22T03:36:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=6474"},"modified":"2021-11-22T18:53:39","modified_gmt":"2021-11-22T13:23:39","slug":"python-get-last-modification-date-time-of-a-file-os-stat-os-path-getmtime","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-get-last-modification-date-time-of-a-file-os-stat-os-path-getmtime\/","title":{"rendered":"Python : Get Last Modification date & time of a file. | os.stat() | os.path.getmtime()"},"content":{"rendered":"
\u00a0<\/strong>We will see how we can get the modification date & time of a file and in our desired formats.<\/p>\n The above function returns To get the modification time, we have to use the Get last modification time of a file using os.path.getmtime() :<\/strong><\/p>\n We can also use another one of python\u2019s os module function i.e. The function returns the number of seconds elapsed after the modification of the file .We have to convert it into a proper format.<\/p>\n The function Also we can set the format in We can also find the modification time of the file without using To obtain the last modification time, we can use <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Getting the Last Modification date & time of a file. | os.stat() | os.path.getmtime() in Python. \u00a0We 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 …<\/p>\nGet last modification time of a file using os.stat( ) :<\/h3>\n
Syntax-os.stat(filePath)<\/pre>\n
os.stat_result<\/code> that contains stats about the file .<\/p>\n
ST_MTIME<\/code><\/em> that will provide us with the modification time in seconds. We will pass it to a function
time.ctime( )<\/code> <\/em>which will return the info in a readable manner.<\/p>\n
import os\r\nimport time\r\nimport stat\r\n#Path of the file\r\nfileStats = os.stat ( 'file.txt' )\r\n#Passing the stats so that we get it in a readable manner\r\nmodTime = time.ctime ( fileStats [ stat.ST_MTIME ] )\r\nprint(\"Modified Time : \", modTime )\r\n<\/pre>\n
Output :\r\nModified Time :\u00a0 Thu May 13 19:02:47 2021<\/pre>\n
os.path.getmtime( )<\/code><\/p>\n
Syntax-os.path.getmtime( filePath)<\/pre>\n
\n
Get last modification time using os.path.getmtime() & time.localtime( ) :<\/strong><\/h4>\n<\/li>\n<\/ul>\n
import os\r\nimport time\r\n#Path of the file going inside the function that returns the number of seconds elpased since modification\r\nmodTimeInSeconds = os.path.getmtime ( 'file.txt' )\r\n#Passing the stats so that we get the modification time in a readable manner\r\nmodTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(modTimeInSeconds))\r\nprint(\"Modified Time : \", modTime )\r\n<\/pre>\n
Output:\r\nModified Time :\u00a0 2021-05-13 19:02:47<\/pre>\n
time.localtime( )<\/code> converts the seconds to a
strruct_time<\/code> which when passed into
strftime( )<\/code> returns the timestamp in readable format.<\/p>\n
strftime( )<\/code> to get only the modification date.<\/p>\n
import os\r\nimport time\r\n#Path of the file going inside the function that returns the number of seconds elpased since modification\r\nmodTimeInSeconds = os.path.getmtime ( 'file.txt' )\r\n#Passing the stats so that we get the modification time in a readable manner\r\nmodTime = time.strftime('%d\/%m\/%Y',time.localtime(modTimeInSeconds))\r\nprint(\"Modified Time : \", modTime )\r\n<\/pre>\n
Output :\r\nModified Time :\u00a0 13\/05\/2021<\/pre>\n
\n
Get last modification time using os.path.getmtime() & datetime.fromtimestamp() :<\/strong><\/h4>\n<\/li>\n<\/ul>\n
time.localtime()<\/code> using
datetime.fromtimestamp()<\/code>.<\/p>\n
import os\r\nimport time\r\nimport datetime\r\n#Path of the file going inside the function that returns the number of seconds elpased since modification\r\nmodTimeInSeconds = os.path.getmtime ( 'file.txt' )\r\n#Passing the stats so that we get the modification time in a readable manner\r\nmodTime = datetime.datetime.fromtimestamp(modTimeInSeconds).strftime('%Y-%m-%d %H:%M:%S')\r\nprint(\"Modified Time : \", modTime )\r\n<\/pre>\n
Output :\r\nModified Time :\u00a0 2021-05-13 19:02:47<\/pre>\n
\n
Get last modification time of a file in UTC Timezone :<\/strong><\/h4>\n<\/li>\n<\/ul>\n
datetime.utcfromtimestamp( )<\/code><\/p>\n
import os\r\nimport time\r\nimport datetime\r\n#Path of the file going inside the function that returns the number of seconds elpased since modification\r\nmodTimeInSeconds = os.path.getmtime ( 'file.txt' )\r\n#Passing the stats so that we get the modification time in a readable manner\r\nmodTime = datetime.datetime.utcfromtimestamp(modTimeInSeconds).strftime('%Y-%m-%d %H:%M:%S')\r\nprint(\"Modified Time : \", modTime )\r\n<\/pre>\n
Output :\r\nModified Time :\u00a0 2021-05-13 13:32:47<\/pre>\n