python自動ファイル分類


# -*- coding: utf-8 -*-
"""
Created on Thu Aug 16 22:15:00 2018

@author: chenze
"""


import os

import shutil

import re

def mkdir(path):
    folder = os.path.exists(path)
    if not folder:                   #                     
        os.makedirs(path)            #makedirs                    

path='I:/data'#     
newpath='I:/data/  '
pathyear = os.listdir(path)
print(pathyear)
for ye in pathyear:
    #print(ye)
    pathmonth = os.listdir(path+'/'+str(ye))
    for mo in pathmonth:
        #print(mo)
        pathday = os.listdir(path+'/'+str(ye)+'/'+str(mo))
        #print(pathday)
        old_path=path+'/'+str(ye)+'/'+str(mo)
        print(old_path)
        for da in pathday:
            year = da[15:19]
            month = da[19:21]
            day = da[21:23]
            new_dir=newpath+'/'+str(year)+' '+str(month)+' '+str(day)+' '
            mkdir(new_dir)
            new_path_files=os.path.join(new_dir,da) ##     ,   
            old_path_files=os.path.join(old_path,da)
            shutil.move(old_path_files, new_path_files) ##         ,  
            ##shutil.copy()     ,  copy