Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see if file is older than 3 months in Python?

Tags:

python

time

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.

like image 992
Luke B Avatar asked Apr 27 '11 03:04

Luke B


People also ask

How can I tell when a python file was last modified?

Python's os. path. getmtime() method can be used to determine when the given path was last modified.

How do you know if time is greater than in Python?

You can use greater than operator > to check if one datetime object is greater than other.

How can I tell when a python file was created?

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().


1 Answers

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
like image 166
Senthil Kumaran Avatar answered Sep 27 '22 22:09

Senthil Kumaran