Find the age of a file in Python

Hi, welcome to this tutorial. Here we will learn about finding the age of the given file in Python.

To find the age of a file, we require the last modification time.

In PYTHON, we have modules named OS which come under standard utility module. This module provides a way to interact with the operating system through its various functions.

To know more about the function provided by the OS module, click on the below link.

OS Module in Python.

Our goal is to find the age of a file. To do this we need to know the Current time. Finally, subtracting the last modification time from the Current time gives the age of the given file.

To know the current time we need to simply import the TIME module.

The TIME module provides various operations to access the time, to do conversions, etc.

To learn more about the TIME module, just click on the below link.

Time module in Python.

How to find the age of a file in Python

Now lets code to get the age of the file.

import os 
import time 
print("The age of the given file is: ",Result)


The age of the given file is: 22827208.57


In our code, we have imported the required modules OS and TIME that make our task easier.

We have used the stat function of the OS Module. Through this function, we can know the status of the file ‘/bin’ as a parameter. We have used the variable ‘x’ to store the status of the given file.

The current time is known by calling the time method of the TIME module.

st_mtime is one of the attributes from the resultant of the stat() method. The st_mtime shows the time of the last modification. unit is: seconds.

We have used another variable “Result” to store the age of the file. Finally, by using the print statement we can print the AGE of the given file.

Leave a Reply

Your email address will not be published. Required fields are marked *