使用python多进程快速复制文件夹

发布时间:2020-07-25 12:24:05 作者:xu360176091
来源:网络 阅读:539

from multiprocessing import Pool,Manager
import os,sys
import time
import argparse

def recvParameter():
parse = argparse.ArgumentParser()
parse.add_argument("-s","--src",dest="src_base_folder",required=True,help="Enter source folder path")
parse.add_argument("-d","--des",dest="des_base_folder",required=True,help="Enter destination folder path")
parse.add_argument("-p","--pcount",dest="pcount",type=int,default=5,help="The count of process")
args = parse.parse_args()
if not (os.path.isdir(args.src_base_folder) and os.path.isdir(args.des_base_folder)):
print("%s or %s is not exists" %(args.src_base_folder,args.des_base_folder))
sys.exit(-1)
return args

def getTotalSize(dir):
size = 0
for root, dirs, files in os.walk(dir):
size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
return size

def printProcessBar(l):
rate = (sum(l)-l[0])/l[0]
print("\r %.2f%%" %(rate*100),end="")

def fileHandle(fileFullName,src_base_folder,des_base_folder,l):
src_file = fileFullName
des_file = fileFullName.replace(src_base_folder,des_base_folder)
des_file_folder = os.path.dirname(des_file)
if not os.path.exists(des_file_folder):
os.makedirs(des_file_folder)
fw = open(des_file,"w+")
with open(src_file,"r") as f:
while True:
content = f.read(1024)
if not content:
break
fw.write(content)
fw.close()
l.append(os.path.getsize(src_file))
return l

def main():
args = recvParameter()
src_base_folder=args.src_base_folder
des_base_folder=args.des_base_folder
pool = Pool(args.pcount)
begintime = time.time()
manager=Manager()
d = manager.dict()
l = manager.list()
l.append(getTotalSize(src_base_folder))
for base_path,dirs,files in os.walk(src_base_folder):
for file in files:
fileFullName = os.path.join(base_path,file)
pool.apply_async(func=fileHandle,args=(fileFullName,src_base_folder,des_base_folder,l),callback=printProcessBar)

pool.close()
pool.join()
endtime = time.time()
print("\ntotalTime=%s" %(endtime-begintime))

if name == "main":
main()

推荐阅读:
  1. python多进程使用函数封装
  2. python中文件和文件夹复制函数是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 多进程 pool

上一篇:oracle权限的分配与回收

下一篇: IP首部校验和的计算

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》