python-文件夹的复制

src_path = r'd:\ppython\p1' #初始位置
tar_path = r'd:\ppython\p2' #目标位置

#函数
def copy(src,tar):
	if os.path.isdir(src) and os.path.isdir(tar):
		filelist = os.listdir(src)

		for file in filelist:
			path = os.path.join(src,file)
			tar2 = os.path.join(tar,file)
			if os.path.isdir(path):
				os.mkdir(tar2)
				copy(path,tar2)
			else:
				with open(path,'rb') as rstream:
					container = rstream.read()
					with open(tar2,'wb') as wstream:
						wstream.write(container)
		else:
			print('复制完毕')

#调用函数
copy(src_path,tar_path)
posted @ 2020-05-28 10:00  Akmf's_blog  阅读(86)  评论(0)    收藏  举报