Python os.path.getmtime() Method

Python os.path.getmtime() Method

  1. Syntax of os.path.getmtime() Method
  2. Example Code: Use the os.path.getmtime() Method to Get Modification Time
  3. Example Code: The os.path.getmtime() Method Returns FileNotFoundError
  4. Example Code: Handle the os.path.getmtime() Method FileNotFoundError Exception
  5. Example Code: Difference Between os.path.getmtime() and os.path.getctime() Methods
  6. Example Code: Use the datetime.now() Method to Get Time Difference Between Two Timestamps

The os.path.getmtime() method returns time (in seconds) for directory and path of file. The output time of the modified file is shown in the form of seconds on the console.

Syntax of os.path.getmtime() Method

os.path.getmtime(path)

Parameters

path The specified file or directory path to get the modification time.

Return

This method returns the seconds as a floating point value of the modified file time. We can change that number to a local time stamp using the ctime() method.

Example Code: Use the os.path.getmtime() Method to Get Modification Time

#importing os and time module
import os
import time

#Path of the directory
file_path = 'C:/Users/lenovo/Downloads/'
#returns the time of last modification in directory
file_modification_time = os.path.getmtime(file_path)
print("Last modification time of the directory: ", file_modification_time)
#conversion of seconds to Local Time format
local_time = time.ctime(file_modification_time)
print("Last modification time in Date Time format: ", local_time)
#path of the file
file_path = 'C:/Users/lenovo/Desktop/myfile.txt'
file_modification_time = os.path.getmtime(file_path)
print("Last modification time of the file: ", file_modification_time)
local_time = time.ctime(file_modification_time)
print("Last modification time in Date Time format: ", local_time)

Output:

Last modification time of the directory:  1660051538.7374249
Last modification time in Date Time format:  Tue Aug  9 18:25:38 2022
Last modification time of the file:  1660051176.3274927
Last modification time in Date Time format:  Tue Aug  9 18:19:36 2022

The method returns the floating value of modification in seconds. We have converted the seconds to the Local Date Time with the ctime() method.

Example Code: The os.path.getmtime() Method Returns FileNotFoundError

import os
#Path of the file
file_path = 'C:/Users/lenovo/Downloads/myfile.txt'
file_modification_time = os.path.getmtime(file_path)
print("Last modification time of the directory: ", file_modification_time)

Output:

FileNotFoundError: The system cannot find the file specified: 'C:/Users/lenovo/Downloads/myfile.txt'

The os.path.getmtime() method returns an error exception when the specified path does not exist.

Example Code: Handle the os.path.getmtime() Method FileNotFoundError Exception

import os
import sys
import time
# Path of the file
path = 'C:/Users/lenovo/Downloads/myfile.txt'
#try/except exception handling
try:
    file_modification_time = os.path.getmtime(path)
    print("Last modification time of the file:", file_modification_time)
except OSError:
    print("Path '%s' does not exist. Please recheck the path." %path)
    #exit the program if the file does not exist. If you remove this line, then the source code will show the error
    sys.exit()

time = time.ctime(file_modification_time)
print("Last modification in Local time:", time)

Output:

Path 'C:/Users/lenovo/Downloads/myfile.txt' does not exist. Please recheck the path.

The method returns the exception error if the file is not found. We used the try and OSError exception to handle the exception.

Example Code: Difference Between os.path.getmtime() and os.path.getctime() Methods

import os
import time

file_path = 'C:/Users/lenovo/Downloads/'
file_modification_time = os.path.getctime(file_path)
print("Last modification time of the directory: ", file_modification_time)
local_time = time.ctime(file_modification_time)
print("Last modification time in Date Time format: ", local_time)

file_path = 'C:/Users/lenovo/Desktop/myfile.txt'
#returns file creation time (never changes)
file_modification_time = os.path.getctime(file_path)
print("Last modification time of the file: ", file_modification_time)
local_time = time.ctime(file_modification_time)
print("Last modification time in Date Time format: ", local_time)

Output:

Last modification time of the directory:  1625313864.4088457
Last modification time in Date Time format:  Sat Jul  3 17:04:24 2021
Last modification time of the file:  1660051269.1368756
Last modification time in Date Time format:  Tue Aug  9 18:21:09 2022

As we have discussed earlier, the os.path.getmtime() method returns the time of modification of a file or directory, and the os.path.getctime() returns the creation time of the file or path.

Example Code: Use the datetime.now() Method to Get Time Difference Between Two Timestamps

import os
from datetime import datetime
#olson tz database in python and supports all the time zones
import pytz
#returns the datetime zone for right now
def getDateTime(timezone):
    tz = pytz.timezone(timezone)
    #return a DateTime occurrence according to the mentioned time zone parameter tz
    dateTime = datetime.now().astimezone()
    return dateTime.astimezone(tz)
#returns the datetime of creation of the file
def timestamp(timestamp, timezone):
    tz = pytz.timezone(timezone)
    dateTime = datetime.fromtimestamp(timestamp).astimezone()
    return dateTime.astimezone(tz)
# write your time zone here
time_zone = 'Asia/Karachi'

file_timestamp = os.path.getmtime('C:/Users/lenovo/Desktop/myfile.txt')

currentDateTime = getDateTime(time_zone)
print("Current time stamp of the device: ",currentDateTime)
file_datetime = timestamp(file_timestamp, time_zone)
print("Creation time of the specified file: ",file_datetime)

#difference in both times
time_difference = currentDateTime - file_datetime
print("Difference of time stamp between the above two times: ",time_difference)

Output:

Current time stamp of the device:  2022-08-09 23:10:06.151655+05:00
Creation time of the specified file:  2022-08-09 21:37:11.666461+05:00
Difference of time stamp between the above two times:  1:32:54.485194

We used two methods getDateTime() and timestamp(). The code above will return the difference in time between the two timestamps.

Author: Junaid Khan
Junaid Khan avatar Junaid Khan avatar

Hi, I'm Junaid. I am a freelance software developer and a content writer. For the last 3 years, I have been working and coding with Python. Additionally, I have a huge interest in developing native and hybrid mobile applications.

LinkedIn

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method