Pythonマルチプロセスはフォルダ内のファイルを再帰的にコピーします

12429 ワード

import multiprocessing
import os
import re
import time

#       、       
SOUR_PATH = ""
DEST_PATH = ""
#            
SOUR_FILE_LIST = list()
SOUR_DIR_LIST = list()


def traverse(source_path):
    """        ,       、    

    :param source_path:         
    """
    if os.path.isdir(source_path):
        SOUR_DIR_LIST.append(source_path)
        for temp in os.listdir(source_path):
            new_source_path = os.path.join(source_path, temp)
            traverse(new_source_path)
    else:
        SOUR_FILE_LIST.append(source_path)


def copy_files(queue, sour_file, dest_file):
    """                

    :param queue:   ,      
    :param sour_file:
    :param dest_file:
    """
    # time.sleep(0.1)
    try:
        old_f = open(sour_file, "rb")
        new_f = open(dest_file, "wb")
    except Exception as ret:
        print(ret)
    else:
        content = old_f.read()
        new_f.write(content)
        old_f.close()
        new_f.close()

    queue.put(sour_file)


def main():
    source_path = input("              :
"
) SOUR_PATH = source_path DEST_PATH = SOUR_PATH + "[ ]" # dest_path = input(" ") # DEST_PATH = dest_path print(">>> :", SOUR_PATH) print("> :", DEST_PATH) print(" ...") queue = multiprocessing.Manager().Queue() po = multiprocessing.Pool(5) traverse(source_path) print(" ...") for sour_dir in SOUR_DIR_LIST: dest_dir = sour_dir.replace(SOUR_PATH, DEST_PATH) try: os.mkdir(dest_dir) except Exception as ret: print(ret) else: print("\r %s " % DEST_PATH, end="") print() print(" ") for sour_file in SOUR_FILE_LIST: dest_file = sour_file.replace(SOUR_PATH, DEST_PATH) po.apply_async(copy_files, args=(queue, sour_file, dest_file)) count_file = len(SOUR_FILE_LIST) count = 0 while True: q_sour_file = queue.get() if q_sour_file in SOUR_FILE_LIST: count += 1 rate = count * 100 / count_file print("\r : %.2f%% %s" % (rate, q_sour_file), end="") if rate >= 100: break print() ret = re.match(r".*\\([^\\]+)", SOUR_PATH) name = ret.group(1) print(" %s " % name) if __name__ == '__main__': main()