The following code selects a random sample of files (in this case 50) from dir 1 and copies them to a new folder with the same name.
However, I have hundreds of folders which I need to sample from (and copy to a new folder with the same name).
How can I adjust the first part of the code so that I can loop through all sub directories, and move the samples to a new folder with the same name. (so the sample of sub dir 1 goes to dir 1, the sample of sub dir 2 goes to dir 2 etc.)
import os
import shutil
import random
from shutil import copyfile
sourcedir = '/home/mrman/dataset-python/train/1/'
newdir = '/home/mrman/dataset-python/sub-train/1'
filenames = random.sample(os.listdir(sourcedir), 50)
for i in filenames:
shutil.copy2(sourcedir + i, newdir)