python整体拷贝包含多个子文件夹的文件夹
Python中的shutil.copy
只能拷贝文件,shutil.copytree
所拷贝的对应目录必须是新建的,不能同时拷贝多个子文件夹到同一个文件夹内
这里使用终端系统命令
import os
# 原始目录src_dir, 目标目录dst_dir
os.system('cp -R %s %s'% (src_dir,dst_dir))
以下是根据原始数据目录source_file_path, 对文件夹ann_path中集中处理过的source_file_path数据整体分别拷贝至原始前二级对应目录下归类(其实这一步在数据处理时就可以做,当时为了方便就没管那么多,然后现在给自己添加工作量。。。)
# -*- coding: utf-8 -*-
# @Time : 2022/2/16 13:42
# @Author : yusuen
import os
import glob
ann_path = '/path/'
all_file_list = os.listdir(ann_path)
# print(len(all_file_list))
source_file_path = glob.glob('/path/*/*/*.mp4')
# print(len(source_file_path))
data_root = 'root path'
for file_path in source_file_path:
split_path= file_path.split('/')
name = split_path[-1]
name = name.split('.')[0]
new_path = split_path[5] + '/' + split_path[6] + '/'
dest_path = data_root + new_path
src_path = ann_path + name + '/'
# print('dest', dest_path)
# print('src', src_path)
# 创建文件夹
if not os.path.exists(dest_path):
os.makedirs(dest_path)
# 文件夹名称中包含空格需要在空格前加上\
src_path = src_path.replace(' ','\ ')
dest_path = dest_path.replace(' ', '\ ')
os.system('cp -R %s %s' % (src_path, dest_path))
print(src_path, '>>>>>>>>>>', dest_path, 'Finished!')