Python复制整个目录和文件内容

import shutil
def copy_dir(src_path,target_path):
    filelist_src = os.listdir(src_path)#用于返回一个文件名和目录名
    for file in filelist_src:#遍历所有的文件或文件夹
        src_path_read_new =os.path.join(os.path.abspath(src_path),file)
        target_path_write_new = os.path.join(os.path.abspath(target_path),file)
        if os.path.isdir(src_path_read_new):#判断该读入路径是否是目录文件夹,如果是文件夹执行递归
            if not os.path.exists(target_path_write_new):#判断目标路径是否存在该文件夹
                os.mkdir(target_path_write_new)#没有就创建文件夹
            copy_dir(src_path_read_new,target_path_write_new)#递归
        else:#如果是文件,执行复制
            shutil.copy(src_path_read_new,target_path_write_new)

猜你喜欢

转载自blog.csdn.net/jiaqi_ge/article/details/109339876