0

I am developing a program in python to download a list of files. The are named as:

2012-01-01-01.html
2012-01-01-02.html
...
...
2012-12-30-99.html

The problem is I don't Know whether the file exists. So I have to check all the urls from 404 How can i make a generator that increments the counter for 'year'-'month'-'date'-'num if one link fails then skip the date and if all links of particular month fails skip month and so on I can only write code that is having lot of 'if's is there any better idea

wset_fail=[]
for year in [2012,2011]:
    for month in range(1,12):
        for day in range(1,31):
            for num in range(1,100):   
                check = str(year)+'-'+str(month)+'-'+str(day)
                if check not in wset_fail:
                    link = generate_name(year,month,day,num)
                    if link!='':
                        if download(link)==False:   
                            wset_fail.append(str(year)+'-'+str(month)+'-'+str(day))

Its Working I am asking for some better solution

Sreevisakh
  • 1,896
  • 2
  • 16
  • 23
  • 1
    a simple three nested for loops with a single if would work. Show us what you tried and why it failed. – PA. Apr 08 '12 at 06:55
  • 1
    You want `range(1, 13)` and `range(1, 32)`. You also want to reuse `check` instead of recalculating `str(year)+…`. Furthermore, the test `check not in wset_fail` can be removed, since all `check` values are new, in the innermost loop. – Eric O. Lebigot Apr 08 '12 at 09:11
  • check checks whether set of files for a particular file is already failed – Sreevisakh Apr 09 '12 at 07:13

1 Answers1

1

Make a separate generator for dates as described at Iterating through a range of dates in Python. Then use the dates to download files.

Community
  • 1
  • 1
skrat
  • 5,518
  • 3
  • 32
  • 48