Luke B Luke B - 1 month ago 4x
Python Question

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

I'm curious about manipulating time in Python. I can get the (last modified) age of a file using the

function as such:

import os.path, time


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.


If you need to have the exact number of days you can use the calendar module in conjunction with datetime, e.g.,

import calendar
import datetime

def total_number_of_days(number_of_months=3):
    c = calendar.Calendar()
    d =
    total = 0
    for offset in range(0, number_of_months):
        current_month = d.month - offset
        while current_month <= 0:
            current_month = 12 + current_month
        days_in_month = len( filter(lambda x: x != 0, c.itermonthdays(d.year, current_month)))
        total = total + days_in_month
    return total

And then feed the result of total_number_of_days() into the code that others have provided for the date arithmetic.