Import libraries

import os import shutil

Define directory structure

parent = '/data/VDC/VDC_Caf_Clean/' train = '/data/VDC/split/train/' val = '/data/VDC/split/val/'

Split each directory into training and validation

for dirs in os.listdir(parent): for i, cat in enumerate(os.listdir(parent + dirs)): if not os.path.exists(train + dirs): os.makedirs(train + dirs) if not os.path.exists(val + dirs): os.makedirs(val + dirs) if (i+1) % 4 == 0: print ('Copying to validation directory') shutil.copy(parent + '/' + cat, val + dirs + '/') else: print ('Copying to training directory') shutil.copy(parent + '/' + cat, train + dirs + '/')