import os import shutil # todo python 递归算法 def copy(src,dest): files=os.listdir(src) os.mkdir(dest) for file in files: src_file_path= os.path.join(src,file) dest_file_path=os.path.join(dest,file) if os.path.isfile(src_file_path): with open(src_file_path,"rb")as rs: reader_stream=rs.read() with open(dest_file_path,"wb")as ws: ws.write(reader_stream) else: # is dir copy(src_file_path,dest_file_path)
方法2:
使用shutil
#赋值文件以及空目录
shutil.copyfile(src,dest)
# 递归复制目录以及目录文件
shutil.copytree(src,dest)