import os
import multiprocessing
def copy_file(q,file_name, old_folder_name, new_folder_name):
"""完成檔案的複製"""
old_f = open(old_folder_name+"/"+file_name, "rb")
content = old_f.read()
old_f.close()
new_f = open(new_folder_name+"/"+file_name, "wb")
new_f.write(content)
new_f.close()
# print("從%s--->到%s 複製檔案%s成功!" % (old_folder_name, new_folder_name, file_name))
q.put(file_name)
def main():
# 1.獲取使用者要copy的資料夾名稱
old_folder_name = input("請輸入要copy的資料夾:")
# 2.建立乙個新的資料夾
try:
new_folder_name = old_folder_name + "復件"
os.mkdir(new_folder_name)
except:
pass
# 3.獲取所有待copy的檔名稱 listdir()
file_names = os.listdir(old_folder_name)
print(file_names)
# 4.建立程序池
po = multiprocessing.pool(2)
# 5.建立乙個佇列
q = multiprocessing.manager().queue()
# 6.向程序池中新增copy檔案的任務
for file_name in file_names:
po.close()
# po.join()
all_file_num = len(file_names) # 統計所有的檔案個數
copy_ok_num = 0
while true:
file_name = q.get()
# print("已經完成copy :%s" % file_name)
copy_ok_num += 1
print("\r複製的進度為:%.2f %%" % (copy_ok_num*100/all_file_num),end='')
python多執行緒案例之多工copy檔案完整例項
import os import multiprocessing def copy file q,file name,old folder name,new folder name 完成檔案的複製 old f open old folder name file name,rb content old...
python多工資料夾copy顯示進度
import os import multiprocessing def copy file q,file name,old folder name,new folder name 完成檔案的複製 print 模擬copy檔案 從 s 到 s 檔名是 s old folder name,new fo...
案例 多工udp聊天器
收和發不能同時進行 通過多執行緒來解決 import socket import threading def recv msg udp socket 接收資料並顯示 接收資料 while true recv data udp socket.recvfrom 1024 print recv data ...