您好,登錄后才能下訂單哦!
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()
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。