from multiprocessing import Pool,Managerimport os,sysimport timeimport argparsedef recvParameter():parse = argparse.ArgumentParser()parse.add_argument(-s,--src,dest=src_base_folder,required=Tru...
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多进程快速复制文件夹
基础教程推荐
- CentOS 7.5 安装 Python3.7 2023-09-03
- Python基础学习之函数和代码复用详解 2022-09-02
- 基于Python实现股票数据分析的可视化 2023-08-04
- Python 中 Elias Delta 编码详情 2023-08-08
- ubuntu 18 python3.6 的安装与 python2的版本切换 2023-09-03
- 四步教你学会打包一个新的Python模块 2022-10-20
- Centos7下安装python环境 2023-09-04
- python的环境conda简介 2022-10-20
- Python爬取当网书籍数据并数据可视化展示 2023-08-11
- centos系统 anaconda3(python3)安装pygrib 2023-09-04