how to know when a file is modified in python

You can know when a file was last modified in Python using the os and time modules. Here's an example:

main.py
import os
import time

filename = 'example.txt'

# Get the last modified time for the file
last_modified_time = os.path.getmtime(filename)

# Convert the timestamp to a readable format
readable_time = time.ctime(last_modified_time)

print(f'The file {filename} was last modified at {readable_time}')
300 chars
13 lines

In this example, we start by importing the os and time modules. We then define the name of the file we want to check ('example.txt' in this case).

We use the os.path.getmtime() function to get the timestamp for when the file was last modified. This function returns the time as a Unix timestamp (number of seconds since January 1, 1970).

We then use the time.ctime() function to convert the Unix timestamp to a readable format. This function returns a string representing the time in the format: "Day Month Date HH:MM:SS Year".

Finally, we print out a message indicating when the file was last modified.

gistlibby LogSnag