把所有文件整理到一个目录下

import os
import shutil
path2 = r'\\192.168.1.12\video\rerange\video'
#通过校验MD5 判断B内的文件与A 不同
def get_MD5(file_path):
    files_md5 = os.popen('md5 %s' % file_path).read().strip()
    file_md5 = files_md5.replace('MD5 (%s) = ' % file_path, '')
    return file_md5

def EnumPathFiles(path, callback):
    if not os.path.isdir(path):
        print('Error:"',path,'" is not a directory or does not exist.')
        return
    list_dirs = os.walk(path)

    for root, dirs, files in list_dirs:
        for d in dirs:
            EnumPathFiles(os.path.join(root, d), callback)
        for f in files:
            callback(root,f)
def callback1(path, filename):
    #print(path+'\\'+filename)
    #print(os.path.basename(path))
    full_path = os.path.join(path, filename)
    despath = path2 + '\\' + os.path.basename(path) + filename
    #if get_MD5(full_path) != get_MD5(despath):
    (filename, extension) = os.path.splitext(filename)
    print(full_path)
    print(despath)
    shutil.move(full_path, despath)
    #if extension == '.url' or  extension == '.ini' or  extension == '.php' or  extension == '.chm' or  extension == '.mht' or  extension == '.txt' or  extension == '.URL' or  extension == '.rtf' or extension == '.td':
        #print('delete'+ full_path)
        #os.remove(full_path)
        #

if __name__ == '__main__':
    EnumPathFiles(r'\\192.168.1.12\video\previous', callback1)

你可能感兴趣的:(编程)