Source

woocode / py / si.py

import re
import os

pat = re.compile(r'(\d{4})_(.+)')
filenames = [f for f  in os.listdir('.') if pat.search(f)]
for fn in filenames:
    m = pat.search(fn)
    _d = m.group(1)
    _fn = m.group(2)
    os.makedirs(_d)
    os.rename(fn , os.path.join(_d, _fn))

    print _d, _fn