使用python多进程快速复制文件夹
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()
分享名称:使用python多进程快速复制文件夹
URL网址:http://myzitong.com/article/jjdigi.html