How to use the OS and Time module In Python

We will use the getctime() and getmtime() function, found inside the path module in the os library, for getting the creation and modification times of the file. 

  • getctime()- returns the creation time.
  • getmtime()-returns the last modification time.

Both the above functions return time in seconds since EPOCH (00:00:00 UTC on 1 January 1970). Since that number doesn’t resemble an understandable timestamp, we would have to convert that time so that it becomes recognizable. For that purpose, we would be using the ctime() function found inside the time library.

Example 1: Getting creation and modification time using getctime() and getmtime() methods.

The timestamp of the above code has the following format qualifiers –

[Day](3) [Month](3) [day](2) [Hours:Minutes:Seconds](8) [Year](4)

import os
import time

# Path to the file/directory
path = "myfile.txt"

# Both the variables would contain time
# elapsed since EPOCH in float
ti_c = os.path.getctime(path)
ti_m = os.path.getmtime(path)

# Converting the time in seconds to a timestamp
c_ti = time.ctime(ti_c)
m_ti = time.ctime(ti_m)

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

Output:

The file located at the path myfile.txt was created at Mon Jun 27 14:16:30 2022 and 
was last modified at Thu Jul 7 16:25:52 2022

Where the word inside the bracket is the cue for what is being displayed, and the number following it within the parenthesis displays the length it will occupy.

Example 2: Get creation and modification time in ISO 8601 format

By default, the ctime() function would return a timestamp of the aforementioned syntax. In order to change it, we would have to pass it to strptime() function (also found inside the time library) to create a time structure (object) out of it. Then we can pass format specifiers to strftime(), to create a custom timestamp out of the time structure. In the following code, we will be getting the modification time of the same file in ISO 8601 timestamp format. 

import os
import time

path = r"myfile.txt"

ti_m = os.path.getmtime(path)

m_ti = time.ctime(ti_m)

# Using the timestamp string to create a 
# time object/structure
t_obj = time.strptime(m_ti)

# Transforming the time object to a timestamp 
# of ISO 8601 format
T_stamp = time.strftime("%Y-%m-%d %H:%M:%S", t_obj)

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

Output:

The file located at the path myfile.txt was last modified at 2022-07-07 16:25:52

How to get file creation and modification date or time in Python?

We often need to access various file properties for different purposes. Among the file properties, the creation and modification time of a file are the most commonly used ones. We can achieve the same functionality and use it for various utility purposes, using multiple methods. 

You can use functions from the time module and datetime module to get the file creation and modification of date or time.

Similar Reads

Using the OS and Time module

We will use the getctime() and getmtime() function, found inside the path module in the os library, for getting the creation and modification times of the file....

Using the Datetime Module

Classes for working with date and time are provided by the Python datetime module. Numerous capabilities to deal with dates, times, and time intervals are provided by these classes. Python treats date and datetime as objects, so when you work with them, you’re working with objects rather than strings or timestamps....