How to check file last access time using Python?


The file last access datetime in Python can be obtained in a number of different ways. The following OS module methods will be used to obtain the file last access time in Python.

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 by this approach. The amount of time since the epoch is returned as a floating point value.

It throws one OSError if the requested path cannot be reached or if it does not exist.

Syntax

os.path.getatime(path)

Example - 1

The path to the file whose last access time you want to verify is filepath.

The last access time that we are reading with os.path.getatime is last_access_time.

The datetime module is used to output this time in a human readable format in the final line.

Following is an example to check files last access time using os.path.getatime method −

import os import datetime filepath = r"C:\Users\Lenovo\Downloads\Work TP\trial.py" last_access_time = os.path.getatime(filepath) print('File Last access time is: {}'.format(datetime.datetime.fromtimestamp(last_access_time)))

Output

Following is an output of the above code −

File Last access time is: 2022-07-21 11:25:22.090214

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 readable format by sending that time struct to time.strftime().

We can receive date only and various formats specific to our application by modifying the format string in time.strftime().

The example is as follows −

import os import datetime import time filepath = r"C:\Users\Lenovo\Downloads\Work TP\trial.py" last_access_time_sinceEpoc = os.path.getatime(filepath) LastaccessTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_access_time_sinceEpoc))

Output

Following is an output of the above code −

LastaccessTime
'2022-07-21 11:25:22'

Note − Instead of using time.localtime(), we may also use time.gmtime() to obtain the most recent access time in UTC timezone as given below −

LastaccessTime=time.strftime('%Y%m%d%H:%M:%S',time.gmtime(last_access_time_sinceEpoc))

Using os.stat() Method

It 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

os.stat(filepath)

Example

Access the field st_atime, which holds the time of most recent access in seconds, to obtain the most recent access time from the os.stat result object. Then, using time.ctime, we can convert that to a format that can be read.

Following is an example 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 ("C:\Users\Lenovo\Downloads\Work TP\trial.py") # Get last access time FileLastaccessTime = time.ctime (filePath.st_atime)

Output

Following is an output of the above code.

FileLastaccessTime
'Thu Jul 21 11:25:22 2022'

File last access time in LINUX

Example - 1

We may view a file's access, modification, and change times using the Linux stat command. Simply include a file path in your command −

$ stat code.py

Output

Following is an output of the above command −

File: code.py 
Size: 225                 Blocks: 8          IO Block: 4096     regular file 
Device: 801h/2049d        Inode: 274798       Links: 1 
Access: (0644/-rw-r--r--) Uid: ( 1000/ sarika) Gid: ( 1000/ sarika) 
Access: 2022-07-28 11:50:49.134939844 +0530 
Modify: 2022-07-28 11:50:26.683334414 +0530 
Change: 2022-07-28 11:50:26.683334414 +0530 
 Birth: 2022-07-27 09:59:26.061843845 +0530

Example - 2

Add the −u argument to your command if you want to use ls to view the access time for a file −

$ ls -l code.py

Output

Following is an output of the above command −

-rw-r--r-- 1 sarika sarika 225 Jul 28 11:50 code.py

Updated on: 18-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements