python資料夾copy器(多程序版)

2022-07-06 17:03:09 字數 1517 閱讀 8116

本節的練習的要求如下:

**如下:

import multiprocessing

import os

import time

import random

def copy_file(q, file_name, source_folder_name, dest_folder_name):

"""拷貝檔案"""

# print("正在拷貝檔案:%s,%s ==> %s" % (file_name, source_folder_name, dest_folder_name))

f_read = open(source_folder_name + "/" + file_name, "rb")

f_write = open(dest_folder_name + "/" + file_name, "wb")

while true:

content = f_read.read(1024)

if content:

f_write.write(content)

else:

break

f_write.close()

f_read.close()

time.sleep(random.random())

# 放入已經拷貝完成的檔案

q.put(file_name)

def main():

# 獲取要複製的資料夾名稱

source_folder_name = input("請輸入要複製的資料夾名稱:")

# 目標資料夾

dest_folder_name = source_folder_name + "[副本]"

if not os.path.exists(dest_folder_name):

os.mkdir(dest_folder_name)

# 獲取資料夾中的所有普通檔名

file_names = os.listdir(source_folder_name)

# 建立queue

q = multiprocessing.manager().queue()

# 建立pool

po = multiprocessing.pool(5)

for file_name in file_names:

po.close()

all_file_num = len(file_names)

copy_ok_num = 0

while true:

file_name = q.get()

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

Python資料夾copy器(多程序版)

import multiprocessing import os import time import random defcopy file queue,file name,source folder name,dest folder name copy檔案到指定的路徑 f read open s...

python應用 資料夾copy器(多程序版)

import multiprocessing import os import time import random def copy file queue,file name,source folder name,dest folder name copy檔案到指定的路徑 f read open ...

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...