I have 10 folds, with 2 folders in each fold face
, and background
, I want to copy different folds inside train, validate folders so the train or validate will have face
, background
from different folds.
I tried the following code, but since Google Colab uses python 3.6.9, I can't ignore the error of folder exists (as in python 3.9) so I get the following error:
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/')
The solution was to copy the files one by one recursively:
import os, shutil
for i in [x for x in range(10) if ((x != 9) and (x != 1))]:
# print(fileList[i])
# Train
if(i > 2):
subPathList = glob.glob('/content/NewDataset/'+fileList[i]+'/**/', recursive=False)
for subPath in subPathList:
for im in os.listdir(subPath):
imFullPath = os.path.join(subPath, im)
targetPath = os.path.join('/content/train',subPath.split('/')[-2]+ '/')
# print('Train: ', targetPath)
shutil.copy(imFullPath, targetPath)
# Validate
else:
subPathList = glob.glob('/content/NewDataset/'+fileList[i]+'/**/', recursive=False)
for subPath in subPathList:
for im in os.listdir(subPath):
imFullPath = os.path.join(subPath, im)
targetPath = os.path.join('/content/valid',subPath.split('/')[-2] + '/')
# print('Validate: ', targetPath)
shutil.copy(imFullPath, targetPath)