
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check File Last Access Time Using Python
Monitoring file access times is a common requirement for auditing, data management and cleanup of the scripts. Python provides multiple ways to retrieve the last access time of a file using the os and pathlib modules.
Using os.path.getatime() Method
In Python, we can use the os.path.getatime() method to retrieve a path's most recent access time. The path that we need to verify for the access time is taken as the input argument by os.path.getatime() method. This method returns the amount of time since the epoch, as a floating point value.
It throws one OSError if the requested path cannot be reached or if it does not exist.
Syntax
Following is the syntax of using the os.path.getatime() method -
os.path.getatime(path)
Example 1
Following is an example to check files last access time using os.path.getatime() method -
import os import datetime filepath = r"D:\Tutorialspoint\Articles\sample.py" last_access_time = os.path.getatime(filepath) print('File Last access time is: {}'.format(datetime.datetime.fromtimestamp(last_access_time)))
Following is the output of the above program ?
File Last access time is: 2025-05-13 15:13:57.786038
Example 2
In the following example, filepath stands for the file's path and returns the file's most recent access time in seconds since the epoch. The times since epoch can then be converted to timestamps in other readable formats.
Here, the struct time function of time.localtime() transforms the seconds since the epoch into a local timezone. The timestamp can then be obtained in a readable format by sending that time struct to time.strftime() -
import os import datetime import time filepath = r"D:\Tutorialspoint\Articles\sample.py" last_access_time_sinceEpoc = os.path.getatime(filepath) LastaccessTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_access_time_sinceEpoc)) print("LastaccessTime:", LastaccessTime)
Here is the output of the above program -
LastaccessTime: 2025-05-13 15:18:43
Note: Instead of using time.localtime() method, we can also use time.gmtime() method to obtain the most recent access time in UTC timezone as given below -
import os import datetime import time filepath = r"D:\Tutorialspoint\Articles\sample.py" last_access_time_sinceEpoc = os.path.getatime(filepath) LastaccessTime=time.strftime('%Y%m%d%H:%M:%S',time.gmtime(last_access_time_sinceEpoc)) print("LastaccessTime:", LastaccessTime)
Here is the output of the above program -
LastaccessTime: 2025051309:52:38
Using os.stat() Method
The os.stat() method takes the file's path as an argument and returns the file's status as an os.stat result object. It includes numerous details about the file, like its mode, link type, access or modification time, etc.
Syntax
Here is the syntax of the method os.stat() -
os.stat(filepath)
Example
Following is an example, which shows how to check files' last access time using os.stat method -
import os import stat import time # get the the stat_result object filePath = os.stat (r"D:\Tutorialspoint\Articles\sample.py") # Get last access time FileLastaccessTime = time.ctime (filePath.st_atime) print("LastaccessTime:", FileLastaccessTime)
Following is an output of the above program -
LastaccessTime: Tue May 13 15:34:09 2025
File last access time in LINUX
In Linux, the last access time of a file is also called the time, which represents the last time a file was read. This can be achieved by using the simple command as below -
$ stat filename
Example 1
We may view a file's access, modification, and change times using the Linux stat command. Simply include a file path in our command -
$ stat sample.py
Example 2
When we add the ?u argument to our command, if we want to use ls to view the access time for a file -
$ ls -l code.py