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 String endswith()
Python List append()
Python Set issubset()
Python strptime()
Python eval()
Python Set issuperset()
Python String isspace()
Python Program to Compute all the Permutation of the String
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String title()
Python String index()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python zip()
Python Dictionary values()
Python pass statement
Python any()
Python Exception Handling Using try, except and finally statement
Python Program to Swap Two Variables
Python Dictionary fromkeys()
Python getattr()
Python String rjust()
Python Program to Check If Two Strings are Anagram
Python Program to Slice Lists
Python Dictionary update()
Deep Learning with Python - Francois Cholletf
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python bin()
Deep Learning in Python - LazyProgrammer
Python List Comprehension
Python String isdigit()
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Python Program to Add Two Numbers