使用多进程实现文件夹拷贝

需求:将某个路径中的全部文件(不考虑路径中存在子路径的情况)拷贝到目标路径中
适用环境:python3

实现代码

#coding=utf-8

'''
采用多进程方式实现将某路径下的文件拷贝到另一个路径中
'''

import os
import multiprocessing
import datetime
from functools import wraps

#增加一个装饰器,用于统计函数执行的时间
def logger_time(fn):
    @wraps(fn)
    def wrapper(*args,**kwargs):
        start = datetime.datetime.now()
        ret = fn(*args,**kwargs)
        delta = (datetime.datetime.now()-start).total_seconds()
        print("\n耗时: {}秒".format(delta))
        return ret
    return wrapper


def copyFile(queue,filename,srcpath,dstpath): #队列、文件名、源文件路径、目的文件路径
    srcfile = srcpath+"/"+filename
    dstfile = dstpath+"/"+filename
    srcf_open = open(srcfile,"rb")
    dstf_open = open(dstfile,"wb")
    while True:
        srcf_content = srcf_open.read(1024)
        if srcf_content:
            dstf_open.write(srcf_content)
        else:
            break
    queue.put(filename)
 
       
@logger_time
def main(srcpath,dstpath):
    if os.path.exists(dstpath):
        try:
            os.rmdir(dstpath) # 创建目标文件路径
        except:
            for file in os.listdir(dstpath):
                os.remove(dstpath+"/"+file)  # 如果目标文件路径存在,则先删除里面文件
    else:
        os.mkdir(dstpath)
	# 获取源文件路径中的所有文件名	
    srcfile_names = os.listdir(srcpath)
    # 创建队列 Queue
    queue = multiprocessing.Manager().Queue()
	# 创建一个进程池
    pool = multiprocessing.Pool(5) 
    for filename in srcfile_names:
	    # 向进程池中添加任务
        pool.apply_async(copyFile, args=(queue,filename,srcpath,dstpath))

    pool.close()
	# pool.join()
    srcfile_num = len(srcfile_names)
    copyfile_num = 0
    while True:
        copyfilename = queue.get()
        copyfile_num += 1
        print("\r拷贝进度为:{}%".format(round(copyfile_num*100/srcfile_num,2)),end="")
        if copyfile_num >= srcfile_num:
            break

if __name__=="__main__":
    srcpath = "test"
    dstpath = "test1"
    main(srcpath,dstpath)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jepson2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值