Count file names by name and list by date

I have this code that counts files in a directory with the same two letters. I want to change it so that it does it according to the date of change. 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 result would be as follows:

 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 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 got bogged down in using it and getting it to run. Since I read about regular expressions and how to extract the day of the month in the file name, but mostly confused how all this will be related.

+4
source share
2 answers

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): # sort by file group modification date print mod_date.day for initials, count in sorted(date_counters[mod_date].iteritems(), key=itemgetter(1)): print initials, count 

Produced this output:

 30 bl 1 gr 2 pu 5 31 ye 3 re 4 
+1
source

You can use groupby to organize files:

First you need a function that displays the file in mtime, and then gets a list of files sorted by this value:

 from collections import Counter from itertools import groupby import os import datetime def find_mod_date(basedir): return lambda filename: datetime.date.fromtimestamp( os.stat(os.path.join(basedir, filename)).st_mtime) path="/tmp" mod_dates_in_path = find_mod_date(path) files = [fname for fname in os.listdir(path) if os.path.isfile(os.path.join(path, fname)) and any(name in fname for name in ['red', 'blue'])] files = sorted(files, key=mod_dates_in_path) 

Then group the files by mtime:

 grouping_by_date = groupby(files, key=mod_dates_in_path) 

Iterate over the results and count by name:

 results = {} for day, group in grouping_by_date: results[day] = Counter(name[:2] for name in group) for day, prefix_counts in results.iteritems(): print day for prefix, count in prefix_counts.iteritems(): print "{}: {}".format(prefix, count) 
+1
source

Source: https://habr.com/ru/post/1483869/


All Articles