import os
# 拷贝目录
def copy_dir(src, dst):
# 同一地址无需拷贝
if os.path.abspath(src) == os.path.abspath(dst):
print('地址相同,无需拷贝')
return
# 判断源文件是否是普通文件
if os.path.isfile(src):
print(src, '是文件,无法拷贝')
return
# 目标地址不存在
if not os.path.exists(dst):
os.makedirs(dst)
# 目标地址不是目录
if not os.path.isdir(dst):
print(dst, '目标地址不是目录,无法拷贝')
return
# 目标地址是目录,递归拷贝
dirs = os.listdir(src)
for f in dirs:
# 目录拼接
src_file = os.path.join(src, f)
dst_file = os.path.join(dst, f)
# 分类处理
if os.path.isfile(src_file):
# 是文件,直接拷贝
copy(src_file, dst_file)
else:
# 是目录,递归拷贝
copy_dir(src_file, dst_file)
copy_dir('test', 'test2/ttt')
python 拷贝一个目录
最新推荐文章于 2024-05-14 11:32:26 发布