I'm curious about manipulating time in Python. I can get the (last modified) age of a file using the os.path.getmtime()
function as such:
import os.path, time
os.path.getmtime(oldLoc)
I need to run some kind of test to see whether this time is within the last three months or not, but I'm thoroughly confused by all the available time options in Python.
Can anyone offer any insight? Kind Regards.
Python's os. path. getmtime() method can be used to determine when the given path was last modified.
You can use greater than operator > to check if one datetime object is greater than other.
Use the stat() method of a pathlib object To get the creation and modification time of a file, use the stat( ) method of a pathlib object. This method returns the metadata and various information related to a file, such as file size, creation, and modification time. stat().
You can use a bit of datetime arthimetic here for the sake of clarity.
>>> import datetime
>>> today = datetime.datetime.today()
>>> modified_date = datetime.datetime.fromtimestamp(os.path.getmtime('yourfile'))
>>> duration = today - modified_date
>>> duration.days > 90 # approximation again. there is no direct support for months.
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With