Python | os.path.getmtime() method Last Updated : 10 Oct, 2021 Comments Improve Suggest changes Like Article Like Report OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name manipulation.os.path.getmtime() method in Python is used to get the time of last modification of the specified path. This method returns a floating point value which represents the number of seconds since the epoch. This method raise OSError if the file does not exist or is somehow inaccessible. Note: The epoch represents the point where the time starts. It is platform dependent. For Unix, the epoch is January 1, 1970, 00:00:00 (UTC). Syntax: os.path.getmtime(path)Parameter: path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.Return Type: This method returns a floating-point value of class 'float' that represents the time (in seconds) of last modification of the specified path. Code #1: Use of os.path.getmtime() method Python3 # Python program to explain os.path.getmtime() method # importing os and time module import os import time # Path path = '/home/User/Documents/file.txt' # Get the time of last # modification of the specified # path since the epoch modification_time = os.path.getmtime(path) print("Last modification time since the epoch:", access_time) # convert the time in # seconds since epoch # to local time local_time = time.ctime(modification_time) print("Last modification time(Local time):", local_time) Output: Last modification time since the epoch: 1558447897.0442736 Last modification time (Local time): Tue May 21 19:41:37 2019 Code #2: Handling error while using os.path.getmtime() method Python3 # Python program to explain os.path.getmtime() method # importing os, time and sys module import os import sys import time # Path path = '/home/User/Documents/file2.txt' # Get the time of last # modification of the specified # path since the epoch try: modification_time = os.path.getmtime(path) print("Last modification time since the epoch:", modification_time) except OSError: print("Path '%s' does not exists or is inaccessible" %path) sys.exit() # convert the time in # seconds since epoch # to local time local_time = time.ctime(modification_time) print("Last modification time(Local time):", local_time) # above code will print # path does not exists or is inaccessible' # if the specified path does not # exists or is inaccessible Output: Path '/home/User/Documents/file2.txt' does not exists or is inaccessible Reference: https://docs.python.org/3/library/os.path.html Create Quiz Comment I ihritik Follow 0 Improve I ihritik Follow 0 Improve Article Tags : Python python-os-module Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like