find the last modified date of a json file in python

You can use the os.path.getmtime() function to get the last modified time of a file, and then convert it to a human-readable format using the datetime module.

Here's an example code snippet:

main.py
import os
import json
from datetime import datetime

filename = 'example.json'
mtime = os.path.getmtime(filename)
mtime_str = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')

print(f'The file {filename} was last modified on {mtime_str}.')
251 chars
10 lines

Note that os.path.getmtime() returns the last modified time as a Unix timestamp, which represents the number of seconds since January 1, 1970. The datetime.fromtimestamp() function converts this timestamp to a datetime object, which can then be formatted using the strftime() method.

gistlibby LogSnag