This article uses python to copy folders. At the beginning, I wrote an ordinary recursive copy folder. Then I thought about it. I felt that it was relatively friendly to frequent io programs threading threads. So I wrote a multi-threaded version. The most disgusting thing is the path. Everything else is fine.
import os import threading import multiprocessing length_of_folder = 0 def copyfile(Path): if (Path): print("-----------%s" % ("Testfortherading_" + '/' + Path[length_of_folder:])) ("Testforthreading_" + '/' + Path[length_of_folder:]) filenames = (Path) for filename in filenames: if (Path + '/' + filename): #ps = "Testforthreading_" +"/" + Path[length_of_folder:] #print("%s" % (ps + '/' + filename)) #(ps + '/' + filename) temp = Path + '/' + filename t = (target=copyfile , args=(temp,)) () else: f = open(Path + '/' + filename , 'rb') content = () F = open('Testforthreading_' + '/' + Path[length_of_folder:]+ '/' + filename , 'wb') (content) () () def main(): """""" foldername = input("Please input the folder you want to copy:") length_of_folder = len(foldername) if ("Testforthreading_"): ("Testforthreading_") ("Testforthreading_") copyfile(foldername) #p = (10) #que = ().Queue() if __name__ == "__main__": main()
ps: Python multi-process recursively copy files in folders
import multiprocessing import os import reimport time # Source folder address, destination folder addressSOUR_PATH = "" DEST_PATH = "" # Source File List Folder ListSOUR_FILE_LIST = list() SOUR_DIR_LIST = list() def traverse(source_path): """Recursively traverse the source folder to get the folder list and file list :param source_path: user-specified source path """ if (source_path): SOUR_DIR_LIST.append(source_path) for temp in (source_path): new_source_path = (source_path, temp) traverse(new_source_path) else: SOUR_FILE_LIST.append(source_path) def copy_files(queue, sour_file, dest_file): """Copy files in the file list to the specified folder :param queue: queue, used to monitor progress :param sour_file: :param dest_file: """ # (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() (sour_file) def main(): source_path = input("Please enter the path to the folder you need to copy:\n") SOUR_PATH = source_path DEST_PATH = SOUR_PATH + "[Copy]" # dest_path = input("Please enter the destination folder path") # DEST_PATH = dest_path print(">>> Source folder path:", SOUR_PATH) print(">Target folder path:", DEST_PATH) print("Start calculating files...") queue = ().Queue() po = (5) traverse(source_path) print("Create the target folder...") for sour_dir in SOUR_DIR_LIST: dest_dir = sour_dir.replace(SOUR_PATH, DEST_PATH) try: (dest_dir) except Exception as ret: print(ret) else: print("\rTarget folder %s was created successfully" % DEST_PATH, end="") print() print("Start copying files") 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 = () if q_sour_file in SOUR_FILE_LIST: count += 1 rate = count * 100 / count_file print("\r File Copy Progress: %.2f%% %s" % (rate, q_sour_file), end="") if rate >= 100: break print() ret = (r".*\\([^\\]+)", SOUR_PATH) name = (1) print("Folder %s copy completed" % name) if __name__ == '__main__': main()
Summarize
The above is the multi-threaded recursive copying of folders and files in folders under linux introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!