/  Technology   /  How to get file creation & modification of date/times in Python?
How to get file creation & modification of date/times in Python

How to get file creation & modification of date/times in Python?


A Timestamp is a set of characters that signify the occurrence of an event. These timestamps have varying precision and accuracy, i.e. some timestamps have the time in milliseconds too while others don’t. Due to this varying precision and accuracy, we have different Timestamp formats. 


In this article, let’s learn different methods for finding the creation and modification timestamps of a file.


Using getctime() and getmtime():

These two functions must be imported from the path module present in the os library, which helps in getting the creation and modification times of a file. These functions return time in seconds since EPOCH. The return type of time is float.


import os
import time

file_path = r"C:\Program Files (x86)\Google\pivpT.png"

timestamp_getc = os.path.agtctime(path)
timestamp_getm = os.path.getmtime(path)


As this time in seconds is returned, since EPOCH isn’t an understandable timestamp, we’ll convert it to a much readable and recognizable format using the ctime() function present inside the time library.

Parameter: seconds which is an integer/float value.  

Return: a string which denotes a timestamp.



converted_getc_timestamp = time.ctime(timestamp_getc)
converted_getm_timestamp = time.mtime(timestamp_getm)


And finally print the timestamp using the below print statement.

print(f"The file located at the path {path} was created at {converted_getc_timestamp} and was last modified at {converted_getc_timestamp}")


This is how the output would look like


The above output has the following timestamp format.

[Day] [Month] [day] [Hours:Minutes:Seconds] [Year]
  • [Day] takes 3 characters
  • [Month] takes 3 characters
  • [day] takes 2 characters
  • [Hours:Minutes:Seconds] takes 8 characters
  • [Year] takes 4 characters


Leave a comment