使用多进程拷贝文件

#多进程拷贝文件
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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值