You can know when a file was last modified in Python using the os
and time
modules. Here's an example:
main.py300 chars13 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