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 Variables, Constants and Literals
Python Program to Delete an Element From a Dictionary
Python List index()
Python Program to Check Armstrong Number
Python time Module
How to get current date and time in Python?
Python Program to Add Two Numbers
Python Exception Handling Using try, except and finally statement
Python Data Structures and Algorithms - Benjamin Baka
Python String islower()
Python oct()
Python Dictionary pop()
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Merge Mails
Python Program to Check Whether a String is Palindrome or Not
Python break and continue
Python hasattr()
Python Program to Concatenate Two Lists
Python String join()
Python Dictionary update()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Set copy()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Iterate Over Dictionaries Using for Loop
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
How to Get Started With Python?
Python Program to Find the Factorial of a Number
Python List clear()
Python String isalpha()
Python for Loop
Python String isnumeric()
Python Program to Return Multiple Values From a Function