Rtrader Rtrader - 29 days ago 7
Python Question

Loop through dates except for weekends

So I have a script that has date arguments for different functions and I want it to loop through

not including weekends. Im trying to figure out a way I can use time delta because my script outputs files with the date used in the name of the file for example:

items = (functions.getItems(item,date)
print items
test = sum(abs(l[-1]) for l in items)
total = open('total' +str(datetime.today- datetime.timedelta(1)),'a')

I want timedelta(1) to cycle through each date so that the output file would have the format of
for the first day and cycle through until it created the file
. Also the date argument for items has the format of

I thought that I could do this:

sd = 01-01-2012
ed = 06-09-2012
delta = datetime.timedelta(days=1)
diff = 0
while sd != ed
# do functions
# (have output files (datetime.today - datetime.delta(diff))
diff +=1

So essentially I'm just trying to figure out how can I loop through having the function start with
and ending with
excluding weekends. I'm having trouble figuring out how to exclude weekends and how to get it to loop in the proper order



Use the datetime.weekday() method. It returns values between zero and six, related to the weekdays. Saturday value is 5 and Sunday value is 6; so, if you skip the operation when these values appear, you skip weekdends:

start = datetime(2012, 1, 1)
end = datetime(2012, 10, 6)
delta = timedelta(days=1)
d = start
diff = 0
weekend = set([5, 6])
while d <= end:
    if d.weekday() not in weekend:
        diff += 1
    d += delta