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:
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Intelligent Projects Using Python - Santanu Pattanayak
Python sum()
Python eval()
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Get a Substring of a String
Python @property decorator
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Count the Occurrence of an Item in a List
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String istitle()
Python filter()
Java Program to Implement the Program Used in grep/egrep/fgrep
Python pow()
Python Program to Convert String to Datetime
Python delattr()
Python String casefold()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python List count()
Python if...else Statement
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Get Current time
Python Data Structures and Algorithms - Benjamin Baka
Python Program to Convert Bytes to a String
Python String replace()
Python List append()
Python Program to Access Index of a List Using for Loop
Python Program to Find the Factors of a Number
Python Program to Find the Square Root
Python Set copy()
Python Program to Print Hello world!
Python Program to Find Armstrong Number in an Interval