[Tutor] group txt files by month

Peter Otten __peter__ at web.de
Sat Sep 8 08:03:00 CEST 2012


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, 

Does it throw an exception (if so, post the traceback) or does it just 
terminate?

> 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?


> stop_month = datetime(2011, 12, 31)
> month = datetime(2011, 01, 01)

Hm, 2011 is not a leap year.

> while month < stop_month:
>     accumulate_month(month.year, month.month)
>     month += timedelta(days=32)
>     month = month.replace(day=01)
 
Anyway, this piece of code should work the same for leap years and non-leap 
years. The problem must be in the code you are not posting (or in your 
data).

PS:

> month = datetime(2011, 01, 01)

Stylistic note: numbers starting with 0 are interpreted as octals. It 
doesn't matter here, but don't get into the habit:

>>> 010 == 10
False





More information about the Tutor mailing list