Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
aabbccgithub
Python24
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python24
/
03Thread
/
day02
/
basic04.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
57 lines (46 loc) · 1.78 KB
master
Breadcrumbs
Python24
/
03Thread
/
day02
/
basic04.py
Copy path
Top
File metadata and controls
Code
Blame
57 lines (46 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
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
48
49
50
51
52
53
54
55
56
57
"""
使用**多进程**完成文件夹的copy案例
主进程统计完成的进度,使用进程间的通信queue,子进程完成后放入queue中,主进程去拿
"""
import
os
import
multiprocessing
import
time
def
copy_file
(
src
,
file_name
,
dest
,
queue
):
# 打开一个源文件, 读取文件
src_file
=
open
(
src
+
"/"
+
file_name
,
"rb"
)
# 打开一个目标文件,写入文件
dest_file
=
open
(
dest
+
"/"
+
file_name
,
"wb"
)
file_data
=
src_file
.
read
()
dest_file
.
write
(
file_data
)
# 关闭文件资源
src_file
.
close
()
dest_file
.
close
()
# 通过队列告诉主进程,该子进程已经完成这个文件的copy
queue
.
put
(
file_name
)
def
main
():
# 用户输入需要copy的文件名
src
=
input
(
"请输入你要copy的目录名:"
)
# 创建一个目的地文件夹名
dest
=
src
+
"_备份"
os
.
mkdir
(
dest
)
# 创建一个queue,用来统计进度条
queue
=
multiprocessing
.
Queue
()
# 获取原目录下的文件列表, 遍历获取各个文件名字
for
file_name
in
os
.
listdir
(
src
):
print
(
file_name
)
# 未每一个文件创建一个单独的进程为其负责copy
proc
=
multiprocessing
.
Process
(
target
=
copy_file
,
args
=
(
src
,
file_name
,
dest
,
queue
))
proc
.
start
()
# 使用死循环不停的从queue中读取
count
=
0
while
True
:
queue
.
get
()
count
+=
1
# 转义2个百分号代表一个, \r的作用是每次用这一行输出,配合end="",效果表现为每一次同一行进行文字的更新
print
(
"
\r
当前进度是 %f%%"
%
(
count
/
len
(
os
.
listdir
(
src
))),
end
=
""
)
time
.
sleep
(
0.6
)
if
count
==
len
(
os
.
listdir
(
src
)):
print
(
"
\n
已完成拷贝100%"
)
break
if
__name__
==
'__main__'
:
main
()
You can’t perform that action at this time.