首页 > 编程知识 正文

python 删除目录,python实现文件复制

时间:2023-05-03 23:51:59 阅读:11849 作者:1851

复制文件只是读取和写入文件的操作

defcopy_file(src_path, target_path )文件可能是视频、图像,所以最好在rb和wb上操作)如果文件太大,建议用ab进行写入,一次读取1024字节的FP1=open ) SRC_path。“ ' wb ' ) FP2=open ) target_path,' ab ' ) while true : # content=fp1.read (1024 ) ifno tcontent : break FP2. wrile

遍历sourcePath下的所有子目录和子文件# 1,如果是子文件,则复制文件# 2。 对于子目录,请在目标目录中创建相同的目录名称。 递归调用#注意: rb和wb,因为子文件或子目录的绝对路径importosdefcopy _ file (src _ path target _ path ) : #文件可能是视频、图像一次读取1024字节FP1=open(src_path,' rb ' ) FP2=open(target_path,' ab ' ) while tile (WB ' ) FP2=open ) 来源路径, targetPath ) : # )确定原始目录中是否存在ifnotOS.path.Exists ) : return '目录) #确定目标目录是否存在,如果不存在, 创建ifno tos.path.exists (目标路径) :OS.mkdir (目标路径) list_dir=OS.listdir (源路径)。 fordirinlist _ dir 3360 file _ path1=OS.path.join (source path,dir ) file_path2=OS.path.join ) Targetpath, dir ) ifOS.path.isfile(file_path2) :copy_file ) file_path1, file_path2) else: #在文件夹中递归地进行复制路径(file_path2)的file_path2) if_name_=='_main_':# 将源路径目录的所有内容复制到目标路径目录下的source path=r ' e 3360 _ cath day 13 ' target path=r ' e :cloud music

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。