Table of Contents
In this example, you will learn to get file creation and modification date.
To understand this example, you should have the knowledge of the following Python programming topics:
1. Example 1: Using os module
import os.path, time
file = pathlib.Path('abc.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: Mon Apr 12 10:43:24 2020 Last metadata change time or path creation time: Mon Apr 12 10:43:24 2020
getmtime() gives the last modification time whereas getctime() gives the last metadata change time in Linux/Unix and path creation time in Windows.
2. Example 2: Using stat() method
import datetime
import pathlib
fname = pathlib.Path('abc.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: 2021-04-12 10:43:24.234189 Last metadata change time or path creation time: 2021-04-12 10:43:24.234189
Similar to Example 1, st_mtime refers to the time of last modification; whereas, st_ctime refers to the time of the last metadata change on Linux/Unix and creation time on Windows.
Related posts:
Python break and continue
Python Program to Shuffle Deck of Cards
Python pow()
Python String casefold()
Python File I/O Operation
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python max()
Python vars()
Python Program to Count the Occurrence of an Item in a List
Python if...else Statement
Python Program to Create Pyramid Patterns
Python eval()
Python Set issuperset()
Python isinstance()
Python staticmethod()
Python Program to Remove Punctuations From a String
Python Program to Randomly Select an Element From the List
Python chr()
Python getattr()
Python Objects and Classes
Python Numbers, Type Conversion and Mathematics
Python Program to Find LCM
Python Program to Represent enum
Python str()
Python String istitle()
Python Program to Swap Two Variables
Python Set discard()
Python Program to Convert Celsius To Fahrenheit
Python Statement, Indentation and Comments
Python all()
Python memoryview()
Python Variables, Constants and Literals