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 break and continue
Python String partition()
Python callable()
Python String isalnum()
Python min()
Python Input, Output and Import
Python object()
Python List
Python Program to Measure the Elapsed Time in Python
Python all()
Python Program to Check if a Number is Odd or Even
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python repr()
Python Program to Extract Extension From the File Name
Python Recursion
Python bytes()
Python oct()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Program to Make a Simple Calculator
Node.js vs Python for Backend Development
Python Program to Convert Kilometers to Miles
Python Errors and Built-in Exceptions
Python Program to Create a Long Multiline String
Python Function Arguments
Python Program to Sort Words in Alphabetic Order
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python Program to Create a Countdown Timer
Python List remove()
Python Tuple count()
Python Program to Parse a String to a Float or Int
Python pow()
Python zip()