# 文件复制 (有文件夹)
import os
src_path = r'E:\p1'
target_path = r'E:\p3'
def copy(src_path,target_path):
# 获取文件夹里面的内容
filelist = os.listdir(src_path)
# 遍历列表
for file in filelist:
# 拼接路径
path = os.path.join(src_path,file)
# 判断文件还是文件夹
if os.path.isdir(path):
# 递归调用copy
target_path1 = os.path.join(target_path,file)
os.mkdir(target_path1)
copy(path,target_path1)
else:
# 不是文件夹则直接复制
with open(path,'rb') as rstream:
container = rstream.read()
path1 = os.path.join(target_path,file)
with open(path1,'wb') as wstream:
wstream.write(container)
else:
print('复制完成')
# 调用copy
copy(src_path,target_path)