python 拷贝目录内的所有文件及子目录到另外一个目录

import os
import shutil


A = "Local_Script"
B = "Local_back"
#通过校验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 main(path, out):
    for files in os.listdir(path):
        name = os.path.join(path, files)
        back_name = os.path.join(out, files)
        if os.path.isfile(name):
            if os.path.isfile(back_name):
                if get_MD5(name) != get_MD5(back_name):
                    shutil.copy(name,back_name)     
            else:
                shutil.copy(name, back_name)
        else:
            if not os.path.isdir(back_name):
                os.makedirs(back_name)
            main(name, back_name)


if __name__ == '__main__':
    main(A, B)

#拷贝目录A 的内容到目录B

猜你喜欢

转载自blog.csdn.net/weixin_42934547/article/details/82142476