how to know when a file is modified in python in python

You can use the os.path and os.stat modules to get the last modified time of a file. Here's an example:

main.py
import os
import datetime

file_path = "/path/to/your/file.txt"

# Get the last modified time of the file
modified_time = os.path.getmtime(file_path)

# Convert the modified time into a datetime object
modified_time = datetime.datetime.fromtimestamp(modified_time)

print("The last modified time of the file is:", modified_time)
329 chars
13 lines

This will print the last modified time of the file in a human-readable format. You can also compare the modified time with the current time to see if the file has been modified since the last time you checked:

main.py
current_time = datetime.datetime.now()

if modified_time > current_time:
    print("The file has been modified since the last check.")
else:
    print("The file has not been modified since the last check.")
207 chars
7 lines

gistlibby LogSnag