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:
Node.js vs Python for Backend Development
Python Program to Slice Lists
Python hasattr()
Python object()
Python Program to Convert Two Lists Into a Dictionary
Python Global Keyword
Python for Loop
Python Program to Represent enum
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python hex()
Python List pop()
Python pow()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python locals()
Python Numbers, Type Conversion and Mathematics
How to get current date and time in Python?
Python Program to Sort a Dictionary by Value
Python Inheritance
Python Set symmetric_difference()
Python Program to Create a Long Multiline String
Python Program to Split a List Into Evenly Sized Chunks
Python Data Structures and Algorithms - Benjamin Baka
Python String join()
Python Dictionary setdefault()
Python Set clear()
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Find the Largest Among Three Numbers
Python dir()
Python Program to Multiply Two Matrices
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String capitalize()
Python complex()