Script to restore files to old directory

I thought it would be a great idea to copy all my RAW files into a single directory. Then I changed my mind and thought it would be better if my RAW and JPG files were all together. Instead of trying to work out where the files came from manually, I made a quick script.

import os
import shutil
import fnmatch

source_dir = r'C:\Users\Ed\RAW Pictures'
destination_dir = r'C:\Users\Ed\Pictures'

def locate(pattern, root=os.getcwd()):
    for path, dirs, files in os.walk(root):
        for filename in [os.path.abspath(os.path.join(path, filename)) for filename in files if fnmatch.fnmatch(filename, pattern)]:
            yield filename

for file in os.listdir(source_dir):
    searchstr = os.path.splitext(file)[0] + '.JPG'
    for filematch in locate(searchstr, root=destination_dir):
        print filematch
        shutil.copyfile(os.path.join(source_dir, file), os.path.join(os.path.dirname(filematch), file))

Thanks to this blog for the locate function.