import os.path import scrape_log if __name__ == "__main__": startyear = 2012 endyear=2019 for x in range(startyear,endyear+1): for y in range(1,13): logname = 'output/{0}-{1:02d}.txt'.format(x,y) filenames = map(lambda z : '{0}-{1:02d}-{2:02d}.html'.format(x,y,z), range(1,32)) filenames = filter(os.path.exists, filenames) if len(filenames) > 0: print logname with open(logname, 'w') as s: scrape_log.scrape_from_files(filenames, s)