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:
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Print Output Without a Newline
Python List clear()
Python Program to Add Two Matrices
Python break and continue
Python memoryview()
Python classmethod()
Python Program to Check If a List is Empty
Python vars()
Python issubclass()
Python print()
Python Program to Convert String to Datetime
Python Program to Find Numbers Divisible by Another Number
Python Tuple count()
Python Program to Find the Factorial of a Number
Python Set copy()
Python Program to Count the Number of Occurrence of a Character in String
Python float()
Python Directory and Files Management
Python iter()
Python Program to Count the Number of Each Vowel
Python Program to Create Pyramid Patterns
Python Program to Find Sum of Natural Numbers Using Recursion
Deep Learning with Python - Francois Chollet
Python Dictionary items()
Python String istitle()
Python Tuple index()
Python Program to Remove Punctuations From a String
Python set()
Python String isdecimal()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda