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

<div> </div>
<div>Thanks</div>
<div> </div>
<div> </div>
<div>stop_month = datetime(2011, 12, 31)<br>month = datetime(2011, 01, 01) 
<div class="im"><br>while month < stop_month:<br>    accumulate_month(month.year, month.month)<br>    month += timedelta(days=32)<br></div>    month = month.replace(day=01) </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div class="gmail_quote">On Wed, Apr 11, 2012 at 2:09 PM, questions anon <span dir="ltr"><<a href="mailto:questions.anon@gmail.com" target="_blank">questions.anon@gmail.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote">Thank you for this response it was a tremedous help.<br>It still took me awhile to work it all out and thought I would post what worked for me.<br>
Thanks again<br><br>GLOBTEMPLATE = r"e:/rainfall-{year}/r{year}{month:02}??.txt" 
<div class="im"><br><br>def accumulate_month(year, month):<br>    files = glob.glob(GLOBTEMPLATE.format(year=year, month=month))<br></div>    monthlyrain=[]<br>    for ifile in files:<br>        f=np.genfromtxt(ifile,skip_header=6)<br>
        monthlyrain.append(f)<br>    print "year-month: ",year,"-",month, ", maximum: ", np.max(monthlyrain), "minimum: ", np.min(monthlyrain), "mean: ", np.mean(monthlyrain)<br>
<br>stop_month = datetime(2011, 12, 31)<br>month = datetime(2011, 01, 01) 
<div class="im"><br>while month < stop_month:<br>    accumulate_month(month.year, month.month)<br>    month += timedelta(days=32)<br></div>    month = month.replace(day=01) 
<div class="HOEnZb">
<div class="h5"><br><br></div></div></blockquote>
<div> </div>
<div> </div>
<div> </div>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote">
<div class="HOEnZb">
<div class="h5"><br>
<div class="gmail_quote">On Thu, Apr 5, 2012 at 4:57 PM, Peter Otten <span dir="ltr"><__<a href="mailto:peter__@web.de" target="_blank">peter__@web.de</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT:#ccc 1px solid;MARGIN:0px 0px 0px 0.8ex;PADDING-LEFT:1ex" class="gmail_quote">
<div>
<div>questions anon wrote:<br><br>> I have been able to write up what I want to do (using glob) but I am not<br>> sure how to loop it or simplify it to make the script more efficient.<br>> I am currently:<br>> -grouping the same months in a year using glob<br>
> -opening the files in a group and combining the data using a list<br>> -finding max, min etc for the list and printing it<br>><br>> I need to do this for many years and therefore many months so really need<br>
> a way to make this more efficient.<br>> Any feedback will be greatly appreciated<br>><br>> MainFolder=r"E:/rainfall-2011/"<br>> OutputFolder=r"E:/test_out/"<br>> r201101=glob.glob(MainFolder+"r201101??.txt")<br>
> r201102=glob.glob(MainFolder+"r201102??.txt")<br>> r201103=glob.glob(MainFolder+"r201103??.txt")<br>><br>> rain201101=[]<br>> rain201102=[]<br>> rain201103=[]<br>> monthlyrainfall=[]<br>
><br>> for ifile in r201101:<br>>     f=np.genfromtxt(ifile, skip_header=6)<br>>     rain201101.append(f)<br>><br>> for ifile in r201102:<br>>     f=np.genfromtxt(ifile, skip_header=6)<br>>     rain201102.append(f)<br>
><br>> for ifile in r201103:<br>>     f=np.genfromtxt(ifile, skip_header=6)<br>>     rain201103.append(f)<br>><br>> print "jan", np.max(rain201101), np.min(rain201101), np.mean(rain201101),<br>
> np.median(rain201101), np.std(rain201101)<br>> print "feb", np.max(rain201102), np.min(rain201102), np.mean(rain201102),<br>> np.median(rain201102), np.std(rain201102)<br>> print "mar", np.max(rain201103), np.min(rain201103), np.mean(rain201103),<br>
> np.median(rain201103), np.std(rain201103)<br><br></div></div>Strip the code down to one month<br><br>> r201103=glob.glob(MainFolder+"r201103??.txt")<br>> rain201101=[]<br>
<div>> for ifile in r201101:<br>>     f=np.genfromtxt(ifile, skip_header=6)<br>>     rain201101.append(f)<br><br><br></div>then turn it into a function, roughly<br><br>GLOBTEMPLATE = "e:/rainfall-{year}/r{year}{month:02}??.txt"<br>
def accumulate_month(year, month):<br>   files = glob.glob(GLOBTEMPLATE.format(year=year, month=month))<br>   # read files, caculate and write stats<br><br>and finally put it into a loop:<br><br>from datetime import date, timedelta<br>
stop_month = date(2012, 4, 1)<br>month = datetime(2011, 1, 1)<br>while month < stop_month:<br>   accumulate_month(month.year, month.month)<br>   month += timedelta(days=32)<br>   month = month.replace(day=1)<br>
<div>
<div><br><br><br><br>_______________________________________________<br>Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>To unsubscribe or change subscription options:<br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div></div></blockquote></div><br>