使用多进程拷贝文件

#多进程拷贝文件
import os
import multiprocessing.process


def copy_file(file_name,src_path,dest_path):
    path = src_path + "/" + file_name
    new_path = dest_path + "/" + file_name
    if os.path.isfile(path):
        print(path + "  -->  ./" + new_path)
        r_file = open(path,"rb")
        w_file = open(new_path,"wb")
        while 1:
            data = r_file.read(4096)
            if not data:
                break
            w_file.write(data)
        r_file.close()
        w_file.close()
    else:
        file_list = os.listdir(path)
        os.mkdir(new_path)
        for file_name in file_list:
            copy_file(file_name,path,new_path)

def main():
    src_path = input("请输入要拷贝的文件夹:")
    dest_path = src_path + "-备份"
    if os.path.exists(src_path):
        if not os.path.exists(dest_path):
            os.mkdir(dest_path)
        file_list = os.listdir(src_path)
        pool = multiprocessing.Pool(5)
        for file_name in file_list:
            pool.apply_async(copy_file,args = (file_name,src_path,dest_path))
        pool.close()
        pool.join()
        print("文件拷贝完成")
    else:
        print("要拷贝的文件夹%s不存在!"%src_path)


if __name__ == "__main__":
    main()

你可能感兴趣的:(使用多进程拷贝文件)