find the last modified date of a directory in python

You can use the os.stat method and retrieve the last modification time using st_mtime attribute. Here's an example code block to achieve this:

main.py
import os

directory_path = '/path/to/directory'

# Get the last modified time of the directory
last_modified_time = os.stat(directory_path).st_mtime

# Convert the timestamp to a readable format
formatted_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_modified_time))

print(f"The last modified time of {directory_path} is: {formatted_time}")
359 chars
12 lines

In this code block, we first import the os module. We then set the directory_path variable to the path of the directory we want to check. We then use the os.stat method to get the stat_result object for the directory. We retrieve the last modification time using the st_mtime attribute and store it in last_modified_time variable. Finally, we use the time module to format the last_modified_time into a human-readable format and print the result.

gistlibby LogSnag