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:
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python List clear()
Python Program to Check If a String Is a Number (Float)
Python exec()
Python Set symmetric_difference_update()
Python Program to Find the Sum of Natural Numbers
Python Program to Find HCF or GCD
Python Program to Iterate Over Dictionaries Using for Loop
Python Set issubset()
Python Iterators
Python property()
Python Program to Compute the Power of a Number
Python format()
Python input()
Python Program to Find the Size (Resolution) of a Image
Python Program to Extract Extension From the File Name
Python ord()
Python Generators
Python Modules
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Count the Number of Occurrence of a Character in String
Python String isnumeric()
Python Program to Compute all the Permutation of the String
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Intelligent Projects Using Python - Santanu Pattanayak
Python float()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Randomly Select an Element From the List
Python Dictionary
Python all()
Python String title()
Python Dictionary values()