How to Get the Modified Time of a File using Python

You can use the following template to get the modified time of a file using Python:

import os.path

modified_time = os.path.getmtime(r'path where the file is stored\file name.file extension')

print(modified_time)

The above approach expresses the time in seconds since the epoch.

If you wish to convert the time to a string that represents a local time, then you may use:

import os.path
import time

modified_time = os.path.getmtime(r'path where the file is stored\file name.file extension')
convert_time = time.ctime(modified_time)

print(convert_time)

Later, you’ll also see how to convert the string to a datetime object.

Steps to Get the Modified Time of a File using Python

Step 1: Capture the path where the file is stored

First, capture the path where your file is stored.

For example, let’s suppose that a text file called ‘New_Products‘ is stored under the following path:

C:\Users\Ron\Desktop\Test

Where the modified time of the file is: 2020-12-23 6:34 PM:

New_Products   2020-12-23 6:34 PM

Next, you’ll see how to retrieve that time using Python.

Step 2: Get the modified time of a file using Python

You can use the following template to get the modified time of a file using Python:

import os.path

modified_time = os.path.getmtime(r'path where the file is stored\file name.file extension')

print(modified_time)

For our example:

  • The path where the file is stored is: C:\Users\Ron\Desktop\Test
  • The file name is: New_Products
  • The file extension is: txt

Putting everything together (don’t forget to place the ‘r’ character before your path name):

import os.path

modified_time = os.path.getmtime(r'C:\Users\Ron\Desktop\Test\New_Products.txt')

print(modified_time)
print(type(modified_time))

Here, the time is expressed in seconds since the epoch:

1608766447.5839176
<class 'float'>

Note that the syntax of print(type(modified_time)) was added at the bottom of the code to check the type of the object. In this case, the type of the object is float.

To convert the time above to a string that represents a local time:

import os.path
import time

modified_time = os.path.getmtime(r'C:\Users\Ron\Desktop\Test\New_Products.txt')

convert_time = time.ctime(modified_time)

print(convert_time)
print(type(convert_time))

You’ll now get the string in the following manner:

Wed Dec 23  18:34:07 2020
<class 'str'>

Step 3 (optional): Format the modified time

In order to format the modified_time, for example to ddmmyyyy, you’ll need to:

  1. First, convert to a local time using time.localtime (or to a UTC using time.gmtime)
  2. Then, format the time using time.strftime (the ddmmyyyy can be represented as ‘%d%m%Y’)
import os.path
import time

modified_time = os.path.getmtime(r'C:\Users\Ron\Desktop\Test\New_Products.txt')

convert_time = time.localtime(modified_time)

format_time = time.strftime('%d%m%Y', convert_time)

print(format_time)
print(type(format_time))

As you can see, the new format is ddmmyyyy:

23122020
<class 'str'>

Let’s say that you also want to include the hours, minutes and seconds (in addition to the date). In that case, use ‘%d%m%Y %H:%M:%S‘ as captured below:

import os.path
import time

modified_time = os.path.getmtime(r'C:\Users\Ron\Desktop\Test\New_Products.txt')

convert_time = time.localtime(modified_time)

format_time = time.strftime('%d%m%Y %H:%M:%S', convert_time)

print(format_time)
print(type(format_time))

Here is the result:

23122020 18:34:07
<class 'str'>

Convert to a Datetime Object

What if you want to convert the string that represents the local time into a datetime object?

In that case, you’ll need to import the datetime module, and add the following syntax to the code:

datetime_object = datetime.datetime.strptime(format_time, '%d%m%Y %H:%M:%S')

So the complete code would look like this:

import os.path
import time
import datetime

modified_time = os.path.getmtime(r'C:\Users\Ron\Desktop\Test\New_Products.txt')

convert_time = time.localtime(modified_time)

format_time = time.strftime('%d%m%Y %H:%M:%S', convert_time)

datetime_object = datetime.datetime.strptime(format_time, '%d%m%Y %H:%M:%S')

print(datetime_object)
print(type(datetime_object))

As you may observe, the type of the object is now datetime:

2020-12-23 18:34:07
<class 'datetime.datetime'>

Additional resource

You can check the following documentations about the modules reviewed in this guide: