[Tutor] group txt files by month

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Sep 8 09:20:08 CEST 2012


On 08/09/2012 05:47, questions anon wrote:
> Hello All, it has been a few months since I have used this and I have only
> just realised I am having problems with leap years. each time I get to
> February of a leap year my program stops, therefore I have attributed it to
> my code not accounting for leap years. Is there a simple way to fix my code
> (below) to account for leap years?
>
> Thanks
>
>
> stop_month = datetime(2011, 12, 31)
> month = datetime(2011, 01, 01)
>
> while month < stop_month:
>      accumulate_month(month.year, month.month)
>      month += timedelta(days=32)
>      month = month.replace(day=01)
>

How about this as an alternative, noting that dateutil is a third party 
library and not in the standard library?

from dateutil.relativedelta import relativedelta as rd
def incrMonth(date):
     return date + rd(months = 1)

The same pattern can be used for days, years etc.

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list