I have gone through your snippet.
There are several issues.
Logic is not correct for this part : All images should be checked before the copy, so if they have a zero file length, they will be omitted from the copying process.
Next,
At first , You need to find training and testing length.
Then shuffle.
Then find training set and testing set.