我正在嘗試將大量文件從一個(gè)目錄復(fù)制到另一個(gè)目錄。但是,在嘗試通過(guò)使用Threading加快處理速度時(shí),我收到一個(gè)錯(cuò)誤消息,它抱怨打開(kāi)的文件太多。目前,文件的測(cè)試批次大約為700多個(gè),下面是代碼。我該如何解決?在我的示例中,我將文件從網(wǎng)絡(luò)上的一個(gè)位置復(fù)制到同一網(wǎng)絡(luò)上的另一個(gè)位置,文件范圍從1mb到100mb。def copy_file_to_directory(file, directory): ''' Description: Copies the file to the supplied directory if it exists ''' if os.path.isfile(file): url = os.path.join(directory, os.path.basename(file)) try: shutil.copyfile(file, url) shutil.copystat(file, url) return True except IOError as e: print (e) return Falsedef copy_files_to_directory(files, directory): ''' Directory: Copy a list of files to directory, overwriting existing files ''' if not os.path.isdir(directory): os.makedirs(directory) if not os.path.isdir(directory): return False workers = [] for x in files: if os.path.isfile(x): worker = threading.Thread(target=copy_file_to_directory, args=(x,directory)) workers.append(worker.start()) # wait until they are all done processing for x in workers: x.join() return True files = [] # list of files copy_files_to_directory(files, 'C:/Users/John')
添加回答
舉報(bào)
0/150
提交
取消