python,移动文件到指定目录并排重

def move_file(orgin_path,moved_path):
    dir_files=os.listdir(orgin_path)#得到该文件夹下所有的文件
    for file in  dir_files:
        file_path=os.path.join(orgin_path,file)   #路径拼接成绝对路径
        if os.path.isfile(file_path): #如果是文件,就打印这个文件路径
            if os.path.exists(os.path.join(moved_path,file)):
                print(os.path.join(moved_path,file) + "重复文件!跳过,不移动!" )
                continue
            else:
                shutil.move(file_path, moved_path)
                print("移动文件成功!")
        if os.path.isdir(file_path):  #如果目录,就递归子目录
            childMPath = os.path.join(moved_path,file)
            mkdir(childMPath)
            move_file(file_path,childMPath)
    
def mkdir(path):
    folder = os.path.exists(path)
    if not folder:  #判断是否存在文件夹如果不存在则创建为文件夹
        os.makedirs(path) 

# 调用
oPath = 'C:/xx/res'
mPath = 'D:/xx/res'
move_file(oPath,mPath)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容