find the last accessed date of a directory in python

You can use the os.stat() function to get the statistics of the directory and retrieve the st_atime field, which represents the last time the directory was accessed. You can then convert the timestamp to a human-readable date format using the datetime module.

Here's an example code snippet:

main.py
import os
import datetime

directory_path = '/path/to/directory/'
stat_info = os.stat(directory_path)
last_accessed = stat_info.st_atime
date_time = datetime.datetime.fromtimestamp(last_accessed)
print("Last accessed date:", date_time.strftime('%Y-%m-%d %H:%M:%S'))
266 chars
9 lines

In this code, replace /path/to/directory/ with the actual path of the directory you want to check. The strftime() method formats the datetime object as a string in the format YYYY-MM-DD HH:MM:SS.

gistlibby LogSnag