python多工資料夾copy顯示進度

2021-09-18 00:19:57 字數 1503 閱讀 3652

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_folder_name, file_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()

# 如果拷貝完了檔案,那麼就向佇列中寫入乙個訊息,表示已經完成

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(5)

# 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="")

if copy_ok_num >= all_file_num:

break

print()

if __name__ == "__main__":

main()

多工資料夾複製

高階程式設計技巧 學習筆記 1.1 簡單實現 獲取使用者要複製的資料夾名字 建立乙個新的資料夾 獲取資料夾的所有待拷貝的檔案名字 建立程序池 新增拷貝任務 import multiprocessing import os defcopy file q,file name,old folder nam...

shell按日期刪除資料夾以及檔案,copy即用

情景 需要刪除以201812開頭的 6天前修改的資料夾 資料夾裡包含檔案 find home users niu test log name 201812 type d mtime 5 xargs rm rf amin n 查詢n分鐘以前被訪問過的所有檔案。atime n 查詢n天以前被訪問過的所有...

多工資料夾中的檔案copy(利用佇列和程序池)

1 from multiprocessing import pool 2from multiprocessing import manager 3import os 45 def copy file q,filename,old file name,new file name 6 print 模擬c...