Python Program to Get File Creation and Modification Date – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Get File Creation and Modification Date.

Example 1: Using the os module

# Python Program to Get File Creation and Modification Date

import os.path, time

file = pathlib.Path('img1.jpg')
print("Last modification time: %s" % time.ctime(os.path.getmtime(file)))
print("Last metadata change time or path creation time: %s" % time.ctime(os.path.getctime(file)))

Output

Last modification time: Wed Jan 19 10:34:31 2022
Last metadata change time or path creation time: Sun Oct  2 12:42:37 2022
  • Here we have used the os and time modules to get the last modification time and last metadata change time or path creation time.

Example 2: Using stat() method

import datetime
import pathlib

fname = pathlib.Path('img1.jpg')
print("Last modification time: %s" % datetime.datetime.fromtimestamp(fname.stat().st_mtime))
print("Last metadata change time or path creation time: %s" % datetime.datetime.fromtimestamp(fname.stat().st_ctime))

Output

Last modification time: 2022-01-19 10:34:31.852402
Last metadata change time or path creation time: 2022-10-02 12:42:37.698311
  • Here we have used the stat() method and pathlib module to get the last modification time and last metadata change time or path creation time.

Check out our other python programming examples

Leave a Comment

Your email address will not be published. Required fields are marked *