I have this code that counts files in a directory with the same first two letters. I want to amend it so that it does it by the date modified. So if there were 10 files that started with PR and 10 files that started with FM, 5 each on 5/17/2013 and 5 each on 5/18/2013 the output would be:
17
FM 5
PR 5
18
FM 5
PR 5
import os
from collections import Counter
path = '/My/path/to/the/directory/test'
counts = Counter(fname[:2] for fname in os.listdir(path) if
os.path.isfile(os.path.join(path, fname))
and 'blue' in fname
or 'green' in fname
or 'yellow' in fname
or 'red' in fname
or 'purple' in fname)
for initials, count in counts.most_common():
print '{}: {:>20}'.format(initials,count)
I can print out the date modified, but not in conjunction with the count. I would appreciate any help. I originally wanted to use a scheduler ( have a good example to follow), but bogged down in its usage and getting it to trigger. Since I have been reading about regular expressions and how to extract the day of the month in the filename, but mostly confused as to how it would all connect.
ortests could be simplified to:and any(c in fname for c in ('blue', 'green', 'yellow', 'red', 'purple')(isfile and blue) or ...