Application: folder Copyer (multi process version)
import multiprocessing import os import time import random def copy_file(queue, file_name,source_folder_name, dest_folder_name): """copy File to specified path""" f_read = open(source_folder_name + "/" + file_name, "rb") f_write = open(dest_folder_name + "/" + file_name, "wb") while True: time.sleep(random.random()) content = f_read.read(1024) if content: f_write.write(content) else: break f_read.close() f_write.close() # Send the copied file name queue.put(file_name) def main(): # Get folder to copy source_folder_name = input("Please enter the name of the folder you want to copy:") # Organize destination folder dest_folder_name = source_folder_name + "[copy]" # Create destination folder try: os.mkdir(dest_folder_name) except: pass # If the folder already exists, the creation will fail # Get all the normal file names in this folder file_names = os.listdir(source_folder_name) # Create Queue queue = multiprocessing.Manager().Queue() # Create process pool pool = multiprocessing.Pool(3) for file_name in file_names: # Add tasks to the process pool pool.apply_async(copy_file, args=(queue, file_name, source_folder_name, dest_folder_name)) # Main process display progress pool.close() all_file_num = len(file_names) while True: file_name = queue.get() if file_name in file_names: file_names.remove(file_name) copy_rate = (all_file_num-len(file_names))*100/all_file_num print("\r%.2f...(%s)" % (copy_rate, file_name) + " "*50, end="") if copy_rate >= 100: break print() if __name__ == "__main__": main()
Note: the above content is from the learning notes of the wisdom transfer class. If you need to reprint or complete the notes, please contact me wechat.
Project Python updates my study notes every day. Please refer to the original link for more details
If you have better experience and suggestions, welcome to discuss together. Welcome search official account to join python developer exchange platform.