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 Tuple index()
Python hex()
Python Program to Print all Prime Numbers in an Interval
Python Set difference()
Python Program to Convert Decimal to Binary Using Recursion
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Find Sum of Natural Numbers Using Recursion
Python filter()
Python Program to Transpose a Matrix
Python Program to Make a Simple Calculator
Python String isalpha()
Python iter()
Python abs()
Python Program to Check Leap Year
Python vars()
Python locals()
Python Set intersection()
Python Matrices and NumPy Arrays
Python Program to Add Two Numbers
Python while Loop
Python Program to Multiply Two Matrices
Python Dictionary pop()
Deep Learning with Python - Francois Chollet
Python Program to Measure the Elapsed Time in Python
Python Machine Learning - Sebastian Raschka
Python String maketrans()
Python Program to Slice Lists
Python timestamp to datetime and vice-versa
Python Program to Check If Two Strings are Anagram
Python String rstrip()
Python getattr()
Python datetime