How can i move files in folder that are sorted ? 
<p>This programm sort every file in the folder in new folders with the specific date. 
the main files are like that:</p>

<pre><code>20120509_1324654_house.jpg
20120509_1324654_roof.jpg
</code></pre>

<p>but there are files like that too:</p>

<pre><code>20120509_cat.jpg
20120509_cat.txt
20120509_dog.jpg
20120509_dog.txt
</code></pre>

<p>and these files have to be in the folder "animals", but i dont know how to change my code that the programm create a folder with animals and put them all in there. </p>

<p>this is what I have until now:</p>

<pre><code>from zipfile import ZipFile
import glob
import datetime
import os 
import re
from collections import defaultdict
from shutil import copyfile
pat = r'(\d+)(?:_\d+)?_(\w+?)[\._].*'
from collections import defaultdict

src = 'D:/Testing/src/'

or name in glob.glob('D:/Testing/src/*'): 
    print name

dict_date = defaultdict(lambda : defaultdict(list))
for fil in os.listdir(src):
    if os.path.isfile(os.path.join(src, fil)):
        date, subFolder = re.match(pat, fil).groups()
        dict_date[date][subFolder].append(fil)

for date in dict_date:
        for subFolder in dict_date[date]:
            try:
                os.makedirs(os.path.join(src, date, subFolder))
            except os.error:
                pass
            for fil in dict_date[date][subFolder]:
                copyfile(os.path.join(src, fil), os.path.join(src, date, subFolder, fil))

for date in dict_date:
    with ZipFile(os.path.join(src, '{0}.zip'.format(date)), 'w') as myzip:
        for subFolder in dict_date[date]:
            for fil in os.listdir(os.path.join(src, date, subFolder)):
                if not fil.endswith('.zip'):
                    myzip.write(os.path.join(src, date, subFolder, fil))



directory = src
os.chdir(directory)
files = glob.glob('*.txt')
for filename in files:
    os.unlink(filename)

files2 = glob.glob('*.png')
for filename in files2:
    os.unlink(filename)

print ("\n follows files are now in the folder \n")

for name in glob.glob('D:/Testing/src/*'):  
    print name
</code></pre>
