当前位置: 代码迷 >> 综合 >> Python入门——文件复制
  详细解决方案

Python入门——文件复制

热度:10   发布时间:2024-02-04 15:32:30.0
# 文件复制  (有文件夹)
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):# 递归调用copytarget_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)

 

  相关解决方案