Python Program to Get File Creation and Modification Date – 2024

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

Table of Contents

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

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

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