我有 10 个折叠,每个折叠有 2 个文件夹
face
,并且background
,我想在 train, validate 文件夹中复制不同的折叠,以便 train 或 validate 具有face
不同background
的折叠。我尝试了以下代码,但由于Google Colab使用python 3.6.9,我不能忽略文件夹存在的错误(如在python 3.9中),所以我收到以下错误:
错误:
try:
mkdir(name, mode)
except OSError:
# Cannot rely on checking for EEXIST, since the operating system
FileExistsError: [Errno 17] File exists: 'train/'
我的代码:
#--------------
# Split Dataset
#==============
# we Already have the test set
# !mkdir train
# !mkdir valid
import os, shutil
fileList = os.listdir("NewDataset")
fileList.sort()
for i in [x for x in range(10) if ((x != 9) and (x != 1))]:
# print(fileList[i])
# Train
if(i < 7):
subPathList = glob.glob('NewDataset/'+fileList[i]+'/**/', recursive=False)
for subPath in subPathList:
shutil.copytree(subPath, 'train/')
# Validate
else:
subPathList = glob.glob('NewDataset/'+fileList[i]+'/**/', recursive=False)
for subPath in subPathList:
shutil.copytree(subPath, 'valid/')