Python - group files by modified date [hour]
<p>I'm using the following script to grab all the files in a directory, then filtering them based on their modified date. </p>

<pre><code>dir = '/tmp/whatever'
dir_files = os.listdir(dir)
dir_files.sort(key=lambda x: os.stat(os.path.join(dir, x)).st_mtime)
files = []
for f in dir_files:
    t = os.path.getmtime(dir + '/' + f)
    c = os.path.getctime(dir + '/' + f)
    mod_time = datetime.datetime.fromtimestamp(t)
    created_time = datetime.datetime.fromtimestamp(c)
    if mod_time &gt;= form.cleaned_data['start'].replace(tzinfo=None) and mod_time &lt;= form.cleaned_data['end'].replace(tzinfo=None):
         files.append(f)
return by_hour
</code></pre>

<p>I'm need to go one step further and group the files by the hour in which they where modified. Does anyone know how to do this off the top of their head?</p>

<p>UPDATE: I'd like to have them in a dictionary ({date,hour,files})</p>

<p><strong>UPDATED:</strong> 
Thanks for all your replies!. I tried using the response from david, but when I output the result it looks like below (ie. it's breaking up the filename):</p>

<pre><code>defaultdict(&lt;type 'list'&gt;, {datetime.datetime(2013, 1, 9, 15, 0): ['2', '8', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '5', '1', '8', '4', '3', '.', 'a', 'v', 'i', '2', '9', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '5', '2', '0', '2', '4', '.', 'a', 'v', 'i', '3', '0', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '5', '3', '8', '5', '9', '.', 'a', 'v', 'i', '3', '1', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '5', '4', '1', '2', '4', '.', 'a', 'v', 'i', '3', '2', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '5', '5', '3', '1', '0', '.', 'a', 'v', 'i', '3', '3', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '5', '5', '5', '5', '8', '.', 'a', 'v', 'i'], datetime.datetime(2013, 1, 9, 19, 0): ['6', '1', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '9', '0', '1', '1', '8', '.', 'a', 'v', 'i', '6', '2', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '9', '0', '6', '3', '1', '.', 'a', 'v', 'i', '6', '3', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '9', '1', '4', '1', '5', '.', 'a', 'v', 'i', '6', '4', '-', '2', '0', '1', '3', '0', '1', '0', '9', '1', '9', '2', '2', '3', '3', '.', 'a', 'v', 'i']})
</code></pre>

<p>I was hoping to get it to store the complete file names. Also how would I loop over it and grab the files in each hour and the hour they belong to?</p>

<p>I managed to sort the above out by just changing it to append. However it's not sorted from  the oldest hour to the most recent.</p>

<p>Many thanks,
Ben</p>
