01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
from
multiprocessing
import
Pool,Manager
import
os
def
copyFileTask(name,oldFileName,newFilename,queue):
#print(name)
fr
=
open
(oldFileName
+
"\\"+name,"
r")
fw
=
open
(newFilename
+
"\\"+name, "
w")
fileData
=
fr.read()
fw.write(fileData)
fr.close()
fw.close()
queue.put(name)
def
main():
#0获取文件夹名字
oldName
=
input
(
"请输入文件夹名字:"
)
#1创建一个空文件夹
newName
=
oldName
+
"--备份"
os.mkdir(newName)
#2获取old文件夹所有的文件名
fileNames
=
os.listdir(oldName)
#3多进程的方式copy文件
pool
=
Pool(
5
)
queue
=
Manager().Queue()
#进程池里用消息队列需要用Manager类
i
=
0
for
name
in
fileNames:
pool.apply_async(copyFileTask,args
=
(name,oldName,newName,queue))
i
+
=
1
#4获取备份情况
while
True
:
getName
=
queue.get()
if
getName !
=
"":
print
(
"正在备份-->%s"
%
getName)
i
-
=
1
if
i
=
=
0
:
break
pool.close()
pool.join()
print
(
"备份完毕"
)
if
__name__
=
=
"__main__"
:
main()
|
![](http://www.c0ks.com/data/attachment/forum/201805/29/101106kkis63ilr9ixmgyk.png)
原文地址:http://www.c0ks.com/thread-5428-1-1.html