Get File Creation and Modification Date in Python

(Last Updated On: 12/10/2023)

Python Program to Get File Creation and Modification Date Using OS

# Python Program to Get File Creation and Modification Date Using OS
 
# importing os and time module
import os.path, time

file = pathlib.Path('test.py')
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: Tue Apr 18 10:43:24 2023
Last metadata change time or path creation time: Tue Apr 18 10:43:24 2023

Python Program to Get File Creation and Modification Date Using stat()

# Python Program to Get File Creation and Modification Date
 
# importing datetime and pathlib module
import datetime
import pathlib

fname = pathlib.Path('file.py')

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: 2023-04-15 10:43:24.234189
Last metadata change time or path creation time: 2023-04-15 10:43:24.234189

About The Author

Leave a Reply