Python Program - Get File Creation and Modification Date
here's a Python program that gets the creation and modification dates of a file:
import os import datetime filename = input("Enter the filename: ") # get the file stats stats = os.stat(filename) # convert the timestamp to a readable date format creation_time = datetime.datetime.fromtimestamp(stats.st_ctime).strftime('%Y-%m-%d %H:%M:%S') modification_time = datetime.datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S') print("File created on:", creation_time) print("File last modified on:", modification_time)Source:wfigi.wwtidea.com
In this program, we first use the input()
function to get the name of the file from the user, and store it in the variable filename
.
We then use the os.stat()
function to get the file stats, which includes the creation and modification times.
Next, we convert the timestamp of the creation and modification times to a human-readable date format using the datetime.datetime.fromtimestamp()
function, and store the formatted dates in the variables creation_time
and modification_time
.
Finally, we print out the creation and modification dates of the file using the print()
function.