python多进程multiprocessing实现拷贝
2022/4/30 7:13:16
本文主要是介绍python多进程multiprocessing实现拷贝,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import os import multiprocessing def copy_file(file_name , source_dir, dest_dir): # 1.拼接源文件路径和目标文件路径 source_path = source_dir + "/" + file_name dest_path = dest_dir + "/" + file_name # 2.打开源文件和目标文件 with open(source_path, "rb") as source_file: with open(dest_path, "wb") as dest_file: # 3.循环读取源文件到目标路径 while True: data = source_file.read(1024) if data: dest_file.write(data) else: break if __name__ == '__main__': #1.定义源文件和目标文件夹 source_dir = "python教学视频" dest_dir = "/home/python/桌面/test" #2.创建目标文件夹 try: os.mkdir(dest_dir) except: print("目标文件夹已经存在") #3. 读取源文件夹的文件列表 file_list = os.listdir(source_dir) #4.遍历文件列表实现拷贝 for file_name in file_list: # 4.使用多进程实现多任务拷贝 sub_process = multiprocessing.Process(target=copy_file,args=(file_name,source_dir,dest_dir)) sub_process.start() ####额外注意,在多进程中。主进程结束,子进程是不会直接结束的。如果想主进程结束,子进程也结束的话,可以添加进程守护!.
这篇关于python多进程multiprocessing实现拷贝的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python