Python program to get the last access time of a path

How to get the last access time of a path in python:

Python os.path sdubmodule provides different methods related to system path. This submodule is under the os module. os module provides different operating system related functions. You can also use other third party libraries to get the same features that this module provides. But os module is preferred because we don’t have to install any library since it comes with python.

To get the last access time of a path in python, we can use the os.path.getatime() method. In this post, we will learn how to use os.path.getatime() method with examples.

Definition of os.path.getatime():

os.path.getatime method is defined as below:


This method takes the path that we need to check for the access time. It returns a floating point value that represents the number of seconds since epoch.

If the given path is not accessible or if it doesn’t exist, it throws one OSError.

Example of os.path.getatime():

Let’s take a look at the below example:

import os
import datetime

given_path = r"C:\Users\cvc\programs\"

access_time = os.path.getatime(given_path)
print('Last access time: {}'.format(datetime.datetime.fromtimestamp(access_time)))

I am running this program in a windows laptop. Here,

  • given_path is the path of the file that you want to check for the last access time.
  • access_time is the last access time that we are reading using os.path.getatime.
  • The last line is printing this time in human readable format by using the datetime module.

If you run this program, it will print the last access time for the file path.

If the file is not available, it will throw FileNotFoundError.

It will print output something like below:

Last access time: 2021-03-20 16:33:07.464021

You can also give the path of the python file. The result will be changed after each execution. It will throw OSError if the file is not accessible.

You might also like: