One approach would be to create a dictionary from the files associated with their modification date, with the corresponding Counter object, similar to what you do in your code. To simplify some things, I also used defaultdict of Counters .
So, given the folder with these files and the dates of the dates in it for testing:
blue1 05/30/2013 06:37 PM green1 05/30/2013 06:37 PM green2 05/30/2013 06:37 PM purple1 05/30/2013 06:37 PM purple2 05/30/2013 06:37 PM purple3 05/30/2013 06:37 PM purple4 05/30/2013 06:37 PM purple5 05/30/2013 06:37 PM red1 05/31/2013 06:38 PM red2 05/31/2013 06:38 PM red3 05/31/2013 06:38 PM red4 05/31/2013 06:38 PM yellow1 05/31/2013 06:38 PM yellow2 05/31/2013 06:38 PM yellow3 05/31/2013 06:38 PM
This code:
from collections import defaultdict, Counter from datetime import date from operator import itemgetter import os COLORS = ('blue', 'green', 'yellow', 'red', 'purple') NUM_LETTERS = 2 path = 'testdir' date_counters = defaultdict(Counter) for filename, filepath in ((name, os.path.join(path, name)) for name in os.listdir(path)): if (os.path.isfile(filepath) and any(color in filename for color in COLORS)): mod_date = date.fromtimestamp(os.stat(filepath).st_mtime) date_counters[mod_date].update((filename[:NUM_LETTERS],)) for mod_date in sorted(date_counters):
Produced this output:
30 bl 1 gr 2 pu 5 31 ye 3 re 4
source share