File_copy函数
将一个文件路径中的所有文件复制到另一个文件夹中.代码:
import os
from pathlib import Path
import glob
from tqdm import tqdm
import shutil
def flatten_recursive(path='coco128'):
"""
将一个文件路径中的所有文件复制到另一个文件夹中 即将image文件和label文件放到一个新文件夹中
Flatten a recursive directory by bringing all files to top level
"""
# Flatten a recursive directory by bringing all files to top level
new_path = Path(f'{str(path)}_flat')
if os.path.exists(new_path):
shutil.rmtree(new_path) # delete output folder
os.makedirs(new_path) # make new output folder
for file in tqdm(glob.glob(f'{str(Path(path))}/**/*.*', recursive=True)):
shutil.copyfile(file, new_path / Path(file).name)
if __name__ == "__main__":
flatten_recursive()